
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 27 17:08:30 2019

@author: swls
"""

import os
import sys
import cv2
import json
import copy
import imageio
from PIL import Image
from multiprocessing import Pool

import re
from tqdm import tqdm
import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa
import imgaug.parameters as iap
from imgaug.augmentables.polys import Polygon, PolygonsOnImage

from tool import via_tool # export PYTHONPATH=$PYTHONPATH:`pwd`


def aug_image_for_ct_meter(data_dict):
    io_images = []
    polygons = []

    for idx, image_path in enumerate(data_dict.keys()):
        img = imageio.imread(image_path)
        # new_img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
        io_images.append(img)

        # polygons.append([ ia.Polygon([(x,y) for x,y in xy] )  for xy in data_dict[image_path]])
        polygons.append([ ia.BoundingBox(x1=boxes[0], y1=boxes[1],x2=boxes[2],y2=boxes[3], label=l) for boxes, l in  data_dict[image_path] ])
        # if idx == 2:
        #     break
    # new_image = ia.imresize_single_image(io_image, 0.5)

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            iaa.OneOf([
                iaa.Crop(percent=(0.05, 0.1)),
                # iaa.Affine(
                #     scale={"x": (0.8, 1.1), "y": (0.9, 1.1)}, # scale images to 80-120% of their size, individually per axis
                #     # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                #     rotate=(-15, 15), # rotate by -45 to +45 degrees
                #     # shear=(-16, 16), # shear by -16 to +16 degrees
                #     # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                #     # cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                #     # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                # )
            ]),
            # iaa.Rot90(1, keep_size=False)
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
            # iaa.Affine(translate_px={"x":(1,5)}),
            # sometimes([

            # ]),
            # sometimes(
            #     [
            #         iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
            # #         
            #         iaa.LinearContrast((0.2,1.5), per_channel=0.5),
            #         iaa.Add((-10, 10), per_channel=0.5),
            #         iaa.SimplexNoiseAlpha(iaa.OneOf([
            #            iaa.EdgeDetect(alpha=(0.1, 0.5)),
            #            iaa.DirectedEdgeDetect(alpha=(0.1, 0.5), direction=(0.0, 1.0))
            #         ])),
                # ]
            # ),
            # iaa.OneOf([
            #     iaa.GaussianBlur((0, 1.0)),
            #     iaa.MedianBlur(k=(1,3)),
            #     iaa.AverageBlur(k=(1,3))
            # ]),
            # iaa.JpegCompression(compression=(80, 95)),
            iaa.OneOf([
                # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                # iaa.Fliplr(1),
                iaa.LinearContrast((0.8,1.3), per_channel=0.5),
                # iaa.Add((-10, 10), per_channel=0.5),
                iaa.Add((-10, 10), per_channel=0),

                # iaa.SimplexNoiseAlpha(iaa.OneOf([
                #     iaa.EdgeDetect(alpha=(0.1, 0.5)),
                #     iaa.DirectedEdgeDetect(alpha=(0.1, 0.5), direction=(0.0, 1.0))
                # ])),
            ]),
            iaa.Fliplr(0.5),
            iaa.Flipud(0.5)

        ],
        random_order=True
    )

    # image_aug, polygon_aug = seq(images=io_images, polygons=polygons)
    image_aug, polygon_aug = seq(images=io_images, bounding_boxes=polygons)
    return image_aug, polygon_aug

    # ia.imshow(new_image)


def aug_image_for_ct_circle_meter(data_dict):
    io_images = []
    polygons = []

    for idx, image_path in enumerate(data_dict.keys()):
        img = imageio.imread(image_path)
        # new_img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
        io_images.append(img)

        # polygons.append([ ia.Polygon([(x,y) for x,y in xy] )  for xy in data_dict[image_path]])
        polygons.append([ ia.BoundingBox(x1=boxes[0], y1=boxes[1],x2=boxes[2],y2=boxes[3], label=l) for boxes, l in  data_dict[image_path] ])
        # if idx == 2:
        #     break
    # new_image = ia.imresize_single_image(io_image, 0.5)

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            # iaa.Rot90(1, keep_size=False)
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
                iaa.Affine(
                    scale={"x": (0.8, 1.1), "y": (0.9, 1.1)}, # scale images to 80-120% of their size, individually per axis
                    # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                    rotate=(-4, 4), # rotate by -45 to +45 degrees
                    # shear=(-16, 16), # shear by -16 to +16 degrees
                    # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                    # cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                    # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                ),
                iaa.LinearContrast((0.8,1.3), per_channel=1),
                iaa.Add((-10, 10), per_channel=1),
            # sometimes([

            # ]),
            # sometimes(
            #     [
            #         iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
            # #         
            #         iaa.LinearContrast((0.2,1.5), per_channel=0.5),
            #         iaa.Add((-10, 10), per_channel=0.5),
            #         iaa.SimplexNoiseAlpha(iaa.OneOf([
            #            iaa.EdgeDetect(alpha=(0.1, 0.5)),
            #            iaa.DirectedEdgeDetect(alpha=(0.1, 0.5), direction=(0.0, 1.0))
            #         ])),
                # ]
            # ),

            # iaa.OneOf([
            #     iaa.GaussianBlur((0, 3.0)),
            #     iaa.MedianBlur(k=(1,3)),
            #     iaa.AverageBlur(k=(1,3))
            # ]),
            # iaa.JpegCompression(compression=(90, 95)),
            # iaa.OneOf([
            #     # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
            #     # iaa.Fliplr(1),
            #     iaa.LinearContrast((0.8,1.3), per_channel=0.5),
            #     iaa.Add((-10, 10), per_channel=0.5),
            #     # iaa.SimplexNoiseAlpha(iaa.OneOf([
            #     #     iaa.EdgeDetect(alpha=(0.1, 0.5)),
            #     #     iaa.DirectedEdgeDetect(alpha=(0.1, 0.5), direction=(0.0, 1.0))
            #     # ])),
            # ]),


        ],
        random_order=True
    )

    # image_aug, polygon_aug = seq(images=io_images, polygons=polygons)
    image_aug, polygon_aug = seq(images=io_images, bounding_boxes=polygons)
    return image_aug, polygon_aug

    # ia.imshow(new_image)


def aug_image_for_egg(data_dict):
    io_images = []
    polygons = []

    for idx, image_path in enumerate(data_dict.keys()):
        img = imageio.imread(image_path)
        # new_img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
        io_images.append(img)

        # polygons.append([ ia.Polygon([(x,y) for x,y in xy] )  for xy in data_dict[image_path]])
        polygons.append([ ia.BoundingBox(x1=boxes[0], y1=boxes[1],x2=boxes[2],y2=boxes[3], label=l) for boxes, l in  data_dict[image_path] ])
        # if idx == 2:
        #     break
    # new_image = ia.imresize_single_image(io_image, 0.5)

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
                # iaa.Affine(
                #     scale={"x": (0.9, 1.1), "y": (0.9, 1.1)}, # scale images to 80-120% of their size, individually per axis
                #     translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                #     rotate=(-20, 20), # rotate by -45 to +45 degrees
                #     shear=(-16, 16), # shear by -16 to +16 degrees
                #     order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                #     cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                #     mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                # ),
                # iaa.Rotate(rotate=(-15, 15)),
                iaa.Rot90(1, keep_size=False),

                iaa.LinearContrast((0.8,1.3), per_channel=1),
                iaa.Add((-10, 10), per_channel=1),
                iaa.Fliplr(0.5),
                iaa.Flipud(0.5),

                iaa.OneOf([
                    iaa.AverageBlur(k=1),
                    iaa.GaussianBlur((0, 3.0)),
                    iaa.MedianBlur(k=(1,3)),
                    iaa.AverageBlur(k=(1,3))
                ]),

                iaa.JpegCompression(compression=(70, 95)),

            # sometimes([

            # ]),
            # sometimes(
            #     [
            #         iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
            # #         
            #         iaa.LinearContrast((0.2,1.5), per_channel=0.5),
            #         iaa.Add((-10, 10), per_channel=0.5),
            #         iaa.SimplexNoiseAlpha(iaa.OneOf([
            #            iaa.EdgeDetect(alpha=(0.1, 0.5)),
            #            iaa.DirectedEdgeDetect(alpha=(0.1, 0.5), direction=(0.0, 1.0))
            #         ])),
                # ]
            # ),


            # iaa.OneOf([
            #     # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
            #     # iaa.Fliplr(1),
            #     iaa.LinearContrast((0.8,1.3), per_channel=0.5),
            #     iaa.Add((-10, 10), per_channel=0.5),
            #     # iaa.SimplexNoiseAlpha(iaa.OneOf([
            #     #     iaa.EdgeDetect(alpha=(0.1, 0.5)),
            #     #     iaa.DirectedEdgeDetect(alpha=(0.1, 0.5), direction=(0.0, 1.0))
            #     # ])),
            # ]),


        ],
        random_order=True
    )

    # image_aug, polygon_aug = seq(images=io_images, polygons=polygons)
    image_aug, polygon_aug = seq(images=io_images, bounding_boxes=polygons)
    return image_aug, polygon_aug

    # ia.imshow(new_image)


def aug_image_for_ringelman(data_dict):
    io_images = []
    polygons = []

    for idx, image_path in enumerate(data_dict.keys()):
        img = imageio.imread(image_path)
        # new_img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
        io_images.append(img)

        # polygons.append([ ia.Polygon([(x,y) for x,y in xy] )  for xy in data_dict[image_path]])
        polygons.append([ ia.BoundingBox(x1=boxes[0], y1=boxes[1],x2=boxes[2],y2=boxes[3], label=l) for boxes, l in  data_dict[image_path] ])
        # if idx == 2:
        #     break
    # new_image = ia.imresize_single_image(io_image, 0.5)

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            iaa.Rot90((1,4), keep_size=False),
            # iaa.AdditiveGaussianNoise(scale=(0.02 * 255)),
            # iaa.Affine(translate_px={"x":(1,5)}),
            # sometimes([

            # ]),
            # sometimes(
            #     [
            #         iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
            # #         
            #         iaa.LinearContrast((0.2,1.5), per_channel=0.5),
            #         iaa.Add((-10, 10), per_channel=0.5),
            #         iaa.SimplexNoiseAlpha(iaa.OneOf([
            #            iaa.EdgeDetect(alpha=(0.1, 0.5)),
            #            iaa.DirectedEdgeDetect(alpha=(0.1, 0.5), direction=(0.0, 1.0))
            #         ])),
                # ]
            # ),
            # iaa.OneOf([
            #     iaa.GaussianBlur((0, 1.0)),
            #     iaa.MedianBlur(k=(1,3)),
            #     iaa.AverageBlur(k=(1,3))
            # ]),
            # iaa.JpegCompression(compression=(90, 95)),
            # iaa.OneOf([
                # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                # iaa.Fliplr(1),
                iaa.LinearContrast((0.8,1.3), per_channel=0.5),
                iaa.Add((-10, 10), per_channel=0.5),
                # iaa.SimplexNoiseAlpha(iaa.OneOf([
                #     iaa.EdgeDetect(alpha=(0.1, 0.5)),
                #     iaa.DirectedEdgeDetect(alpha=(0.1, 0.5), direction=(0.0, 1.0))
                # ])),
            # ]),
            iaa.Fliplr(0.5),
            iaa.Flipud(0.5)

        ],
        random_order=True
    )

    # image_aug, polygon_aug = seq(images=io_images, polygons=polygons)
    image_aug, polygon_aug = seq(images=io_images, bounding_boxes=polygons)
    return image_aug, polygon_aug

    # ia.imshow(new_image)


# 对标记的电表图像进行增强
def aug_image_for_white_black_meter(data_dict):
    io_images = []
    polygons = []

    for idx, image_path in enumerate(data_dict.keys()):
        img = imageio.imread(image_path)
        # new_img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
        io_images.append(img)

        # polygons.append([ ia.Polygon([(x,y) for x,y in xy] )  for xy in data_dict[image_path]])
        polygons.append([ ia.BoundingBox(x1=boxes[0], y1=boxes[1],x2=boxes[2],y2=boxes[3], label=l) for boxes, l in  data_dict[image_path] ])
        # if idx == 2:
        #     break
    # new_image = ia.imresize_single_image(io_image, 0.5)

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            # iaa.Rot90(1, keep_size=False)
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
            # iaa.Affine(translate_px={"x":(1,5)}),
            # sometimes([
            iaa.OneOf([

                iaa.imgcorruptlike.DefocusBlur(severity=2),
                # iaa.imgcorruptlike.ZoomBlur(severity=2),
                # iaa.imgcorruptlike.Fog(severity=1),
                # iaa.imgcorruptlike.Frost(severity=1),
                # iaa.imgcorruptlike.Snow(severity=1),
                # iaa.imgcorruptlike.Spatter(severity=1),
                # iaa.imgcorruptlike.Contrast(severity=1),
                iaa.imgcorruptlike.JpegCompression(severity=1),
                iaa.imgcorruptlike.Pixelate(severity=2),
                # iaa.pillike.FilterBlur(),
                # iaa.pillike.FilterSmooth(),
                # iaa.pillike.FilterSmoothMore(),
                iaa.AveragePooling(2),

                iaa.imgcorruptlike.DefocusBlur(severity=2),
                iaa.pillike.FilterBlur(),
                iaa.pillike.FilterSmooth(),
                iaa.pillike.FilterSmoothMore(),

            ]),
            # ]),
            # sometimes(
            #     [
            #         iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
            # #         
            #         iaa.LinearContrast((0.2,1.5), per_channel=0.5),
            #         iaa.Add((-10, 10), per_channel=0.5),
            #         iaa.SimplexNoiseAlpha(iaa.OneOf([
            #            iaa.EdgeDetect(alpha=(0.1, 0.5)),
            #            iaa.DirectedEdgeDetect(alpha=(0.1, 0.5), direction=(0.0, 1.0))
            #         ])),
                # ]
            # ),
            # iaa.OneOf([
            #     iaa.GaussianBlur((0, 1.0)),
            #     iaa.MedianBlur(k=(1,3)),
            #     iaa.AverageBlur(k=(1,3))
            # ]),
            # iaa.JpegCompression(compression=(80, 95)),
            iaa.OneOf([
                # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                # iaa.Fliplr(1),
                iaa.LinearContrast((0.8,1.3), per_channel=0.5),
                # iaa.Add((-10, 10), per_channel=0.5),
                iaa.Add((-10, 10), per_channel=1),

                # iaa.SimplexNoiseAlpha(iaa.OneOf([
                #     iaa.EdgeDetect(alpha=(0.1, 0.5)),
                #     iaa.DirectedEdgeDetect(alpha=(0.1, 0.5), direction=(0.0, 1.0))
                # ])),
            ]),
            iaa.Fliplr(0.5),
            # iaa.Flipud(0.5)

        ],
        random_order=True
    )

    # image_aug, polygon_aug = seq(images=io_images, polygons=polygons)
    image_aug, polygon_aug = seq(images=io_images, bounding_boxes=polygons)
    return image_aug, polygon_aug

    # ia.imshow(new_image)

 
    
def random_add_angle(text_line_image,pts2, angle_list):

    angle = np.random.choice(angle_list)
    
    new_image = cv2.copyMakeBorder(text_line_image, 500,500,500,500,cv2.BORDER_CONSTANT)
    polygons=[]
    polygons.append([ ia.Polygon([(xy[0]+500,xy[1]+500)  for xy in pts2]) ])
    io_images = [new_image]

    seq = iaa.Sequential(
        [
            iaa.Affine(rotate=angle),
        ],
        random_order=True
    )
    image_aug,new_polygons = seq(images=io_images, polygons=polygons)
    cv_image = image_aug[0]

    gray_img = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
    mask = np.array(gray_img,dtype=np.uint8)
    mask[mask>0] = 255


    pts3 = [[[int(x),int(y)] for x,y in zip(plg.xx, plg.yy)]  for plg in new_polygons[0] ]
    new_pts3 = np.array(pts3[0])
    return cv_image, new_pts3, mask

def random_add_angle_projection( text_line_image, pts2, labels, expand_top_down=200, expand_left_right=50):
    """
    先扩大， 再裁剪还原
    @labels: `list`
        每个轮廓的类别
    @pts2: `list`
        每个轮廓的polygon
    """
    image_array = np.array(text_line_image, dtype=np.uint8)

    new_image = cv2.copyMakeBorder(image_array, expand_top_down,expand_top_down,expand_left_right,expand_left_right,cv2.BORDER_CONSTANT)
    image_array_rbg = new_image[:,:,:3]
    image_array_mask = new_image[:,:,3]

    polygons=[]
    for one_object, label in zip(pts2, labels):
        a = []
        for xy in one_object:
            a.append((xy[0]+expand_left_right,xy[1]+expand_top_down))
        if len(a) != 4:return  None,None,None
        polygons.append(ia.Polygon(a, label))
    polygons = [polygons, polygons]
    io_images = [image_array_rbg, image_array_mask]

    seq = iaa.Sequential(
        [
            iaa.OneOf([
                # iaa.Affine(translate_px={"x":(1,5)}),
                iaa.Affine(
                    scale={"x": (0.8, 1.1), "y": (0.9, 1.1)}, # scale images to 80-120% of their size, individually per axis
                    # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                    rotate=(-7, 8), # rotate by -45 to +45 degrees
                    # shear=(-16, 16), # shear by -16 to +16 degrees
                    # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                    # cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                    # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                ),
                iaa.PerspectiveTransform(scale=(0.01, 0.06)),
            ]),
        ],
        random_order=True,
        # deterministic = True
    ).to_deterministic()

    image_aug = []
    polygon_aug = []
    for batch_img, batch_polygon in  zip(io_images, polygons):
        image_batch,polygon_batch = seq(images=[batch_img], polygons=[batch_polygon])
        image_aug.append(image_batch[0])
        polygon_aug.append(polygon_batch[0])

    cv_image = image_aug[0]
    cv_image_mask = image_aug[1]

    # gray_img = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
    # bin_img = cv2.threshold(gray_img, 1, 255, cv2.THRESH_BINARY)[1]
    # k = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    # # mask = cv2.morphologyEx(bin_img, cv2.MORPH_CLOSE, k)
    # mask = cv2.erode(bin_img, k)

    h_sum_array = np.sum(cv_image_mask, axis=1)
    h_indices = np.where(h_sum_array > 0)[0]
    if len(h_indices ) <2:return None,None,None, None
    start_y = h_indices[0]
    end_y = h_indices[-1]

    v_sum_array = np.sum(cv_image_mask, axis=0)
    v_indices = np.where(v_sum_array > 0)[0]
    if len(v_indices ) <2:return None,None,None, None
    start_x = v_indices[0]
    end_x = v_indices[-1]

    pts3 = [[[int(x)- start_x,int(y)-start_y] for x,y in zip(plg.xx, plg.yy)]  for plg in polygon_aug[0] ]
    new_labels = [plg.label  for plg in polygon_aug[0] ]
    new_pts3 = np.array(pts3)
    cv_image = cv_image[start_y:end_y, start_x:end_x, :]
    mask = cv_image_mask[start_y:end_y, start_x:end_x]

    return cv_image, mask, new_pts3, new_labels


def random_add_angle_projection_for_boeing( text_line_image, pts2, labels, expand_top_down=200, expand_left_right=50):
    """
    先扩大， 再裁剪还原
    @labels: `list`
        每个轮廓的类别
    @pts2: `list`
        每个轮廓的polygon
    """
    image_array = np.array(text_line_image, dtype=np.uint8)

    new_image = cv2.copyMakeBorder(image_array, expand_top_down,expand_top_down,expand_left_right,expand_left_right,cv2.BORDER_CONSTANT)
    image_array_rbg = new_image[:,:,:3]
    image_array_mask = new_image[:,:,3]

    polygons=[]
    for one_object, label in zip(pts2, labels):
        a = []
        for xy in one_object:
            a.append((xy[0]+expand_left_right,xy[1]+expand_top_down))
        if len(a) != 4:return  None,None,None, None
        polygons.append(ia.Polygon(a, label))
    polygons = [polygons, polygons]
    io_images = [image_array_rbg, image_array_mask]

    seq = iaa.Sequential(
        [
            iaa.OneOf([
                # iaa.Affine(translate_px={"x":(1,5)}),
                iaa.Affine(
                    scale={"x": (0.7, 1.2), "y": (0.7, 1.2)}, # scale images to 80-120% of their size, individually per axis
                    # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                    rotate=(-180, 180), # rotate by -45 to +45 degrees
                    # shear=(-16, 16), # shear by -16 to +16 degrees
                    # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                    # cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                    # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                ),
                iaa.PerspectiveTransform(scale=(0.01, 0.09)),
            ]),
        ],
        random_order=True,
        # deterministic = True
    ).to_deterministic()

    image_aug = []
    polygon_aug = []
    for batch_img, batch_polygon in  zip(io_images, polygons):
        image_batch,polygon_batch = seq(images=[batch_img], polygons=[batch_polygon])
        image_aug.append(image_batch[0])
        polygon_aug.append(polygon_batch[0])

    cv_image = image_aug[0]
    cv_image_mask = image_aug[1]

    # gray_img = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
    # bin_img = cv2.threshold(gray_img, 1, 255, cv2.THRESH_BINARY)[1]
    # k = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    # # mask = cv2.morphologyEx(bin_img, cv2.MORPH_CLOSE, k)
    # mask = cv2.erode(bin_img, k)

    h_sum_array = np.sum(cv_image_mask, axis=1)
    h_indices = np.where(h_sum_array > 0)[0]
    if len(h_indices ) <2:return None,None,None, None
    start_y = h_indices[0]
    end_y = h_indices[-1]

    v_sum_array = np.sum(cv_image_mask, axis=0)
    v_indices = np.where(v_sum_array > 0)[0]
    if len(v_indices ) <2:return None,None,None, None
    start_x = v_indices[0]
    end_x = v_indices[-1]

    pts3 = [[[int(x)- start_x,int(y)-start_y] for x,y in zip(plg.xx, plg.yy)]  for plg in polygon_aug[0] ]
    new_labels = [plg.label  for plg in polygon_aug[0] ]
    new_pts3 = np.array(pts3)
    cv_image = cv_image[start_y:end_y, start_x:end_x, :]
    mask = cv_image_mask[start_y:end_y, start_x:end_x]

    return cv_image,mask, new_pts3, new_labels

def random_add_angle_projection_for_meng( text_line_image, pts2, labels, expand_top_down=200, expand_left_right=50):
    """
    先扩大， 再裁剪还原
    @labels: `list`
        每个轮廓的类别
    @pts2: `list`
        每个轮廓的polygon
    """
    image_array = np.array(text_line_image, dtype=np.uint8)

    new_image = cv2.copyMakeBorder(image_array, 
                                    expand_top_down,
                                    expand_top_down,
                                    expand_left_right,
                                    expand_left_right,
                                    cv2.BORDER_CONSTANT)
    image_array_rbg = new_image[:,:,:3]
    image_array_mask = new_image[:,:,3]

    polygons=[]
    for one_object, label in zip(pts2, labels):
        a = []
        for xy in one_object:
            a.append((xy[0]+expand_left_right,xy[1]+expand_top_down))
        if len(a) != 4:return  None,None,None, None
        polygons.append(ia.Polygon(a, label))
    polygons = [polygons, polygons]
    io_images = [image_array_rbg, image_array_mask]

    seq = iaa.Sequential(
        [
            iaa.OneOf([
                iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1)),
                iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),

                # iaa.Affine(translate_px={"x":(1,5)}),
                # iaa.Affine(
                #     # scale={"x": (0.7, 1.2), "y": (0.7, 1.2)}, # scale images to 80-120% of their size, individually per axis
                #     # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                #     rotate=(-1, 2), # rotate by -45 to +45 degrees
                #     # shear=(-16, 16), # shear by -16 to +16 degrees
                #     # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                #     # cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                #     # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                # )
                # iaa.PerspectiveTransform(scale=(0.01, 0.09)),
            ]),
        ],
        random_order=True,
        # deterministic = True
    ).to_deterministic()

    image_aug = []
    polygon_aug = []
    for batch_img, batch_polygon in  zip(io_images, polygons):
        image_batch,polygon_batch = seq(images=[batch_img], polygons=[batch_polygon])
        image_aug.append(image_batch[0])
        polygon_aug.append(polygon_batch[0])

    cv_image = image_aug[0]
    cv_image_mask = image_aug[1]

    # gray_img = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
    # bin_img = cv2.threshold(gray_img, 1, 255, cv2.THRESH_BINARY)[1]
    # k = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    # # mask = cv2.morphologyEx(bin_img, cv2.MORPH_CLOSE, k)
    # mask = cv2.erode(bin_img, k)

    h_sum_array = np.sum(cv_image_mask, axis=1)
    h_indices = np.where(h_sum_array > 0)[0]
    if len(h_indices ) <2:return None,None,None, None
    start_y = h_indices[0]
    end_y = h_indices[-1]

    v_sum_array = np.sum(cv_image_mask, axis=0)
    v_indices = np.where(v_sum_array > 0)[0]
    if len(v_indices ) <2:return None,None,None, None
    start_x = v_indices[0]
    end_x = v_indices[-1]

    pts3 = [[[int(x)- start_x,int(y)-start_y] for x,y in zip(plg.xx, plg.yy)]  for plg in polygon_aug[0] ]
    new_labels = [plg.label  for plg in polygon_aug[0] ]
    new_pts3 = np.array(pts3)
    cv_image = cv_image[start_y:end_y, start_x:end_x, :]
    mask = cv_image_mask[start_y:end_y, start_x:end_x]

    return cv_image, mask, new_pts3, new_labels


def random_add_angle_projection_for_meng_cls_gen( image, mask):
    """
    先扩大， 再裁剪还原
    @labels: `list`
        每个轮廓的类别
    @pts2: `list`
        每个轮廓的polygon
    """

    image = cv2.copyMakeBorder(image, 500, 500, 500, 500, cv2.BORDER_CONSTANT)
    mask = cv2.copyMakeBorder(mask, 500, 500, 500, 500, cv2.BORDER_CONSTANT)

    io_images = [image, mask]

    seq = iaa.Sequential(
        [
            iaa.OneOf([

                # iaa.Affine(translate_px={"x":(1,5)}),
                iaa.Affine(
                    # scale={"x": (0.7, 1.2), "y": (0.7, 1.2)}, # scale images to 80-120% of their size, individually per axis
                    # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                    rotate=(-45, 45), # rotate by -45 to +45 degrees
                    # shear=(-16, 16), # shear by -16 to +16 degrees
                    # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                    # cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                    # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                ),
                iaa.PerspectiveTransform(scale=(0.01, 0.09)),
            ]),
        ],
        random_order=True,
        # deterministic = True
    ).to_deterministic()

    image_aug = []
    for batch_img in  io_images:
        image_batch = seq(images=[batch_img])
        image_aug.append(image_batch[0])

    cv_image = image_aug[0]
    cv_image_mask = image_aug[1]

    # gray_img = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
    # bin_img = cv2.threshold(gray_img, 1, 255, cv2.THRESH_BINARY)[1]
    # k = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    # # mask = cv2.morphologyEx(bin_img, cv2.MORPH_CLOSE, k)
    # mask = cv2.erode(bin_img, k)

    h_sum_array = np.sum(cv_image_mask, axis=1)
    h_indices = np.where(h_sum_array > 0)[0]
    if len(h_indices ) <2:return None,None
    start_y = h_indices[0]
    end_y = h_indices[-1]

    v_sum_array = np.sum(cv_image_mask, axis=0)
    v_indices = np.where(v_sum_array > 0)[0]
    if len(v_indices ) <2:return None,None
    start_x = v_indices[0]
    end_x = v_indices[-1]

    cv_image = cv_image[start_y:end_y, start_x:end_x, :]
    mask = cv_image_mask[start_y:end_y, start_x:end_x]

    return cv_image, mask

def aug_image_for_meng_cls(cv_img):
    """
    """
    seq = iaa.Sequential(
        [
            iaa.OneOf([
                iaa.GaussianBlur((0, 3.0)),
                iaa.MotionBlur(k=3),
                iaa.MedianBlur(k=3),
                iaa.AverageBlur(k=3),
                iaa.imgcorruptlike.DefocusBlur(severity=1),
                iaa.imgcorruptlike.JpegCompression(severity=1),
                iaa.imgcorruptlike.Pixelate(severity=1),
                iaa.pillike.FilterBlur(),
                iaa.pillike.FilterSmooth(),
            ]),
            iaa.Add((-50, 50), per_channel=True),
            iaa.LinearContrast((0.9,1.1), per_channel=0.5),
        ],
        random_order=True,
        # deterministic = True
    )
    cv_image = seq(images=[cv_img])[0]
    return cv_image

def aug_image_for_meng_cls_gen(cv_img):
    """
    """
    seq = iaa.Sequential(
        [
            iaa.OneOf([
                iaa.Pepper(0.2),
                iaa.Snowflakes(flake_size=(0.7, 0.95), density_uniformity=(0.2, 0.6), speed=(0.007, 0.05)),
                iaa.Rain(),
                iaa.imgcorruptlike.Spatter(severity=[1,2]),
            ])
        ],
        random_order=True,
        # deterministic = True
    )
    cv_image = seq(images=[cv_img])[0]
    return cv_image

def aug_image_for_meng_cls_rotate(cv_img):
    """
    """
    new_image = cv2.copyMakeBorder(cv_img, 500,500,500,500,cv2.BORDER_CONSTANT, (0))
    mask_img = np.asarray(new_image, dtype=np.int)
    mask_img = np.sum(mask_img, axis=2)
    mask_img = np.clip(mask_img, 0, 255)
    mask_img[mask_img > 0] = 255
    mask_img = np.asarray(mask_img, dtype=np.uint8)


    seq = iaa.Sequential(
        [
            # iaa.OneOf([
                # iaa.Affine(translate_px={"x":(1,5)}),
                # iaa.Resize(size=[1.0, 1.5]),
                 
                iaa.Affine(
                    scale={"x": (0.95, 1.05), "y": (0.95, 1.05)}, # scale images to 80-120% of their size, individually per axis
                    # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                    rotate=(-3, 3), # rotate by -45 to +45 degrees
                    # shear=(-16, 16), # shear by -16 to +16 degrees
                    # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                    # cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                    # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                ),
                iaa.PerspectiveTransform(scale=(0.01, 0.03), cval=0, keep_size=True, mode=cv2.BORDER_CONSTANT ),
            # ]),
                iaa.OneOf([
                    iaa.GaussianBlur((0, 3.0)),
                    iaa.MotionBlur(k=3),
                    iaa.MedianBlur(k=3),
                    iaa.AverageBlur(k=3),
                    iaa.imgcorruptlike.DefocusBlur(severity=1),
                    # iaa.imgcorruptlike.Fog(severity=1),
                    # iaa.imgcorruptlike.Frost(severity=1),
                    # iaa.imgcorruptlike.Snow(severity=1),
                    # iaa.imgcorruptlike.Spatter(severity=1),
                    # iaa.imgcorruptlike.Contrast(severity=1),
                    iaa.imgcorruptlike.JpegCompression(severity=1),
                    iaa.imgcorruptlike.Pixelate(severity=1),
                    # iaa.pillike.FilterBlur(),
                    # iaa.pillike.FilterSmooth(),
                    # iaa.pillike.FilterSmoothMore(),

                    iaa.pillike.FilterBlur(),
                    iaa.pillike.FilterSmooth(),

                ]),

                # iaa.OneOf([
                #     iaa.MotionBlur(k=3),
                #     iaa.MotionBlur(k=3),
                #     iaa.MedianBlur(k=3),
                #     iaa.AverageBlur(k=3),

                # ]),

                iaa.Add((-50, 50), per_channel=1),
                iaa.LinearContrast((0.9,1.1), per_channel=0.5),

                # iaa.OneOf([
                #     iaa.imgcorruptlike.DefocusBlur(severity=1),
                #     iaa.imgcorruptlike.DefocusBlur(severity=1),
                #     # iaa.imgcorruptlike.Fog(severity=1),
                #     # iaa.imgcorruptlike.Frost(severity=1),
                #     # iaa.imgcorruptlike.Snow(severity=1),
                #     # iaa.imgcorruptlike.Spatter(severity=1),
                #     # iaa.imgcorruptlike.Contrast(severity=1),
                #     iaa.imgcorruptlike.JpegCompression(severity=1),
                #     iaa.imgcorruptlike.Pixelate(severity=1),
                #     # iaa.pillike.FilterBlur(),
                #     # iaa.pillike.FilterSmooth(),
                #     # iaa.pillike.FilterSmoothMore(),
                #     iaa.imgcorruptlike.DefocusBlur(severity=1),
                #     iaa.imgcorruptlike.DefocusBlur(severity=1),
                #     iaa.imgcorruptlike.JpegCompression(severity=1),
                #     iaa.imgcorruptlike.Pixelate(severity=1),

                #     iaa.pillike.FilterBlur(),
                #     iaa.pillike.FilterSmooth(),
                #     iaa.pillike.FilterSmoothMore(),
                    
                # ]),
 
        ],
        random_order=True,
        # deterministic = True
    ).to_deterministic()

    cv_image = seq(images=[new_image])[0]
    cv_mask = seq(images=[mask_img])[0]

    cv_mask = cv2.threshold(cv_mask, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    k = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    cv_mask = cv2.morphologyEx(cv_mask, cv2.MORPH_CLOSE, k)

    h_sum_array = np.sum(cv_mask, axis=1)
    h_indices = np.where(h_sum_array > 0)[0]
    if len(h_indices ) <2:return None,None
    start_y = h_indices[0]
    end_y = h_indices[-1]

    v_sum_array = np.sum(cv_mask, axis=0)
    v_indices = np.where(v_sum_array > 0)[0]
    if len(v_indices ) <2:return None,None
    start_x = v_indices[0]
    end_x = v_indices[-1]

    cv_image = cv_image[start_y:end_y, start_x:end_x]
    cv_mask = cv_mask[start_y:end_y, start_x:end_x]

    return cv_image, cv_mask


def aug_image_for_stamp_recover(cv_img):
    """
    """
    new_image = cv_img
    # new_image = cv2.copyMakeBorder(image_array, 500,500,500,500,cv2.BORDER_CONSTANT)

    io_images = [new_image]

    seq = iaa.Sequential(
        [
            iaa.SomeOf([0, 3], [
                iaa.Salt(0.2),

                iaa.Snowflakes(flake_size=(0.7, 0.95), density_uniformity=(0.2, 0.6), speed=(0.007, 0.05)),
                # iaa.CloudLayer(intensity_mean=[190, 255], 
                #                 intensity_freq_exponent=[-2.5, -1.5], 
                #                 intensity_coarse_scale=[2,8], 
                #                 alpha_min=0.,
                #                 alpha_multiplier=[0.2, 0.4], 
                #                 alpha_size_px_max=1000, 
                #                 alpha_freq_exponent=[-4.0, -1.5], 
                #                 sparsity=0.2, 
                #                 density_multiplier=[0.5, 1.5]),
                iaa.Clouds(),
                # iaa.Rain()
                iaa.imgcorruptlike.Frost(severity=2),
                iaa.imgcorruptlike.Spatter(severity=2),
                # iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)),
                # iaa.Cartoon(),
            ]),
            iaa.OneOf([
                iaa.GaussianBlur((0, 3.0)),
                iaa.imgcorruptlike.DefocusBlur(severity=1),
                iaa.imgcorruptlike.JpegCompression(severity=1),
                iaa.pillike.FilterBlur(),
                iaa.pillike.FilterSmooth(),
                iaa.pillike.FilterSmoothMore(),

            ]),

            iaa.OneOf([
                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=3),
                iaa.MedianBlur(k=3),
                iaa.AverageBlur(k=3),
            ]),

            iaa.Add((-20, 20), per_channel=1),
            iaa.LinearContrast((0.9,1.1), per_channel=0.5),
 
        ],
        random_order=False,
        # deterministic = True
    )
    image_aug = seq(images=io_images)
    cv_image = image_aug[0]

    return cv_image



def aug_image_for_boeing_detect(image, pts2, labels):
    """
    先扩大， 再裁剪还原
    @labels: `list`
        每个轮廓的类别
    @pts2: `list`
        每个轮廓的polygon
    """

    polygons=[]
    for one_object, label in zip(pts2, labels):
        a = []
        for xy in one_object:
            a.append((xy[0],xy[1]))
        if len(a) != 4:return  None,None,None
        polygons.append(ia.Polygon(a, label))
    polygons = [polygons]
    io_images = [image]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            iaa.OneOf([
                iaa.imgcorruptlike.DefocusBlur(severity=1),
                iaa.imgcorruptlike.DefocusBlur(severity=1),
                iaa.imgcorruptlike.DefocusBlur(severity=1),
                iaa.imgcorruptlike.DefocusBlur(severity=1),
                # iaa.imgcorruptlike.Fog(severity=1),
                # iaa.imgcorruptlike.Frost(severity=1),
                # iaa.imgcorruptlike.Snow(severity=1),
                # iaa.imgcorruptlike.Spatter(severity=1),
                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=5),
                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=3),

            ]),
            iaa.Flipud(0.5),
            iaa.Fliplr(0.5),
            iaa.Add((-6, 6), per_channel=0.5),

        ],
        random_order=True
    )
    image_aug, new_polygons = seq(images=io_images, polygons=polygons)
    cv_image = image_aug[0]

    pts3 = [[[int(x),int(y)] for x,y in zip(plg.xx, plg.yy)]  for plg in new_polygons[0] ]
    new_labels = [plg.label  for plg in new_polygons[0] ]

    new_pts3 = np.array(pts3)

    return cv_image, new_pts3, new_labels


def aug_image_smoothing_3(image):

    io_images = [image]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [

            # iaa.SomeOf([0,2],[
            iaa.OneOf([

                # iaa.pillike.EnhanceSharpness(),
                # iaa.pillike.EnhanceBrightness(),
                # iaa.pillike.EnhanceContrast(),
                # iaa.pillike.EnhanceColor(),

                iaa.imgcorruptlike.DefocusBlur(severity=1),
                # iaa.imgcorruptlike.DefocusBlur(severity=2),
                # iaa.imgcorruptlike.ZoomBlur(severity=2),
                # iaa.imgcorruptlike.Fog(severity=1),
                # iaa.imgcorruptlike.Frost(severity=1),
                # iaa.imgcorruptlike.Snow(severity=1),
                # iaa.imgcorruptlike.Spatter(severity=1),
                # iaa.imgcorruptlike.Contrast(severity=1),
                # iaa.imgcorruptlike.JpegCompression(severity=1),
                # iaa.imgcorruptlike.JpegCompression(severity=2),
                # iaa.imgcorruptlike.Pixelate(severity=1),
                # iaa.pillike.FilterBlur(),
                iaa.pillike.FilterSmooth(),
                # iaa.pillike.FilterSmoothMore(),
                iaa.AveragePooling(2),

                iaa.OneOf([
                    # iaa.GaussianBlur((0, 3.0)),
                    iaa.MedianBlur(k=(1,3)),
                    iaa.AverageBlur(k=(1,3)),
                ]),

                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=5),

            ]),


            # iaa.OneOf([

            #     iaa.imgcorruptlike.DefocusBlur(severity=1),
            #     iaa.pillike.FilterBlur(),
            #     iaa.pillike.FilterSmooth(),
            #     # iaa.pillike.FilterSmoothMore(),

            # ]),

            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),

            # iaa.SomeOf([0,2],[
            #     iaa.OneOf([
            #         iaa.GaussianBlur((0, 2.0)),
            #         iaa.MedianBlur(k=(1,3)),
            #         iaa.AverageBlur(k=(1,3)),
            #     ]),

            #     iaa.MotionBlur(k=3),
            #     iaa.MotionBlur(k=5),

            #     # iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.5, 1.0)),
            #     # iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))
            #     # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
            #     # iaa.Flipud(1),
            #     # iaa.Fliplr(1),
            #     # iaa.LinearContrast((0.8,1.1), per_channel=0.5),
            # ]),

        ],
        random_order=True
    )
    image_aug = seq(images=io_images)
    # pil_image = Image.fromarray(image_aug[0])
    cv_image = image_aug[0]
    return cv_image


def aug_image_smoothing_2(image):

    io_images = [image]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
            iaa.SomeOf([1,2],[
                iaa.OneOf([
                    # iaa.GaussianBlur((0, 2.0)),
                    iaa.MedianBlur(k=(1,3)),
                    iaa.AverageBlur(k=(1,3)),
                ]),

                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=5),

                # iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.5, 1.0)),
                # iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))
                # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                # iaa.Flipud(1),
                # iaa.Fliplr(1),
                iaa.LinearContrast((0.8,1.1), per_channel=0.5),
                iaa.Add((-6, 6), per_channel=0.5),
            ]),

        ],
        random_order=True
    )
    image_aug = seq(images=io_images)
    # pil_image = Image.fromarray(image_aug[0])
    cv_image = image_aug[0]
    return cv_image

def aug_image_smoothing(image):

    io_images = [image]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
            # iaa.SomeOf([0,2],[
            iaa.SomeOf([0,2],[

                iaa.OneOf([
                    iaa.GaussianBlur((0, 2.0)),
                    iaa.MedianBlur(k=(1,3)),
                    iaa.AverageBlur(k=(1,3)),
                ]),

                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=5),

                # iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.5, 1.0)),
                # iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))
                # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                # iaa.Flipud(1),
                # iaa.Fliplr(1),
                iaa.LinearContrast((0.8,1.1), per_channel=0.5),
                iaa.Add((-10, 10), per_channel=0.5),
            ]),

        ],
        random_order=True
    )
    image_aug = seq(images=io_images)
    # pil_image = Image.fromarray(image_aug[0])
    cv_image = image_aug[0]
    return cv_image


def aug_image_smoothing_for_handwrite(image):

    io_images = [image]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
            iaa.SomeOf([1,4],[
                iaa.GaussianBlur((0, 3.0)),
                iaa.MedianBlur(k=(1,3)),
                iaa.AverageBlur(k=(1,3)),
                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=5),

                # iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.5, 1.0)),
                # iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))
                # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                # iaa.Flipud(1),
                # iaa.Fliplr(1),
                iaa.LinearContrast((0.8,1.1), per_channel=0.5),
            ]),
        iaa.Add((0, 17), per_channel=0.5),

        ],
        random_order=True
    )
    image_aug = seq(images=io_images)
    # pil_image = Image.fromarray(image_aug[0])
    cv_image = image_aug[0]
    return cv_image


def aug_image_for_plate_v2(text_line_image, pts2, labels):
    """
    应用于车牌

    在`pts2`最后添加车牌 `mask` 坐标, 将车牌与车牌数字合并在一起, 保存到json中

    先扩大， 再裁剪还原
    @labels: `list`
        每个轮廓的类别
    """
    image_array = np.array(text_line_image)[:,:,:3][:,:,::-1]
    new_image = cv2.copyMakeBorder(image_array, 500,500,500,500,cv2.BORDER_CONSTANT)

    polygons=[]
    for one_object, label in zip(pts2, labels):
        a = []
        for xy in one_object:
            a.append((xy[0]+500,xy[1]+500))
        if len(a) != 4:return  None,None,None, None
        polygons.append(ia.Polygon(a, label))
    polygons = [polygons]
    io_images = [new_image]

    seq = iaa.Sequential(
        [
            # iaa.OneOf([
                # iaa.Affine(translate_px={"x":(1,5)}),
                # iaa.Resize(size=[1.0, 1.5]), 
                iaa.Affine(
                    scale={"x": (0.8, 1.1), "y": (0.9, 1.1)}, # scale images to 80-120% of their size, individually per axis
                    # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                    rotate=(-3, 3), # rotate by -45 to +45 degrees
                    # shear=(-16, 16), # shear by -16 to +16 degrees
                    # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                    # cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                    # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                ),
                iaa.PerspectiveTransform(scale=(0.01, 0.2), cval=0, mode=cv2.BORDER_CONSTANT ),
            # ]),

 
        ],
        random_order=True,
        # deterministic = True
    )
    image_aug,new_polygons = seq(images=io_images, polygons=polygons)
    cv_image = image_aug[0]

    gray_img = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
    bin_img = cv2.threshold(gray_img, 1, 255, cv2.THRESH_BINARY)[1]
    k = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10))
    mask = cv2.morphologyEx(bin_img, cv2.MORPH_CLOSE, k)
    mask = cv2.erode(mask, k)


    h_sum_array = np.sum(bin_img, axis=1)
    h_indices = np.where(h_sum_array > 0)[0]
    if len(h_indices ) <2:return None,None,None, None
    start_y = h_indices[0]
    end_y = h_indices[-1]

    v_sum_array = np.sum(bin_img, axis=0)
    v_indices = np.where(v_sum_array > 0)[0]
    if len(v_indices ) <2:return None,None,None, None
    start_x = v_indices[0]
    end_x = v_indices[-1]

    pts3 = [[[int(x) - start_x,int(y)-start_y] for x,y in zip(plg.xx, plg.yy)]  for plg in new_polygons[0] ]
    # 添加最后一个整体mask
    pts3.append([[0,0],[end_x-start_x, 0],[end_x-start_x, end_y-start_y],[0, end_y-start_y]])
    new_labels = [plg.label  for plg in new_polygons[0] ]
    new_labels.append("plate")

    new_pts3 = np.array(pts3)
    cv_image = cv_image[start_y:end_y, start_x:end_x]
    mask = mask[start_y:end_y, start_x:end_x]

    return cv_image,mask, new_pts3, new_labels


def aug_image_for_boeing(cv_image, pts2):
    """
    旋转 缩放
    """
    new_image = cv2.copyMakeBorder(cv_image, 500,500,500,500,cv2.BORDER_CONSTANT)

    polygons=[]
    a = []
    for xy in pts2:
        a.append((xy[0]+500,xy[1]+500))
    polygons.append(ia.Polygon(a))
    polygons = [polygons]
    io_images = [new_image]

    seq = iaa.Sequential(
        [
            # iaa.OneOf([
                # iaa.Affine(translate_px={"x":(1,5)}),
                # iaa.Resize(size=[1.0, 1.5]), 
                iaa.Affine(
                    scale={"x": (0.8, 1.1), "y": (0.9, 1.1)}, # scale images to 80-120% of their size, individually per axis
                    # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                    rotate=(-45, 45), # rotate by -45 to +45 degrees
                    # shear=(-16, 16), # shear by -16 to +16 degrees
                    # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                    # cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                    # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                ),

                iaa.SomeOf([0,3],[

                    iaa.PerspectiveTransform(scale=(0.01, 0.2), cval=0, mode=cv2.BORDER_CONSTANT ),
                    iaa.Fliplr(1),
                    iaa.Flipud(1),
                ]),

            # ]),

 
        ],
        random_order=True,
        # deterministic = True
    )
    image_aug,new_polygons = seq(images=io_images, polygons=polygons)
    cv_image = image_aug[0]

    gray_img = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
    bin_img = cv2.threshold(gray_img, 1, 255, cv2.THRESH_BINARY)[1]
    k = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    mask = cv2.morphologyEx(bin_img, cv2.MORPH_CLOSE, k)
    mask = cv2.erode(mask, k)


    h_sum_array = np.sum(bin_img, axis=1)
    h_indices = np.where(h_sum_array > 0)[0]
    if len(h_indices ) <2:return None,None
    start_y = h_indices[0]
    end_y = h_indices[-1]

    v_sum_array = np.sum(bin_img, axis=0)
    v_indices = np.where(v_sum_array > 0)[0]
    if len(v_indices ) <2:return None,None
    start_x = v_indices[0]
    end_x = v_indices[-1]

    pts3 = [[[int(x) - start_x,int(y)-start_y] for x,y in zip(plg.xx, plg.yy)]  for plg in new_polygons[0] ]


    new_pts3 = np.array(pts3)
    cv_image = cv_image[start_y:end_y, start_x:end_x]

    return cv_image, new_pts3[0]



def aug_image_for_plate(image):


    io_images = [image]
    # polygons.append([ ia.BoundingBox(x1=boxes[0], y1=boxes[1],x2=boxes[2],y2=boxes[3]) for boxes in  data_dict[image_path] ])


    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            iaa.OneOf([
                # iaa.OneOf([
                #     iaa.OneOf([
                #         iaa.MotionBlur(k=3),
                #         iaa.MotionBlur(k=5),
                #     ]),
                #     iaa.JpegCompression(compression=(60, 61)),
                # ]),
                iaa.SomeOf([1,2], [
                    # iaa.OneOf([
                    #     iaa.MotionBlur(k=3),
                    #     iaa.MotionBlur(k=5),
                    #     # iaa.GaussianBlur(3.0),
                    # ]),
                    iaa.MotionBlur(k=3),
                    iaa.MotionBlur(k=5),
                    iaa.JpegCompression(compression=(50, 81)),
                ]),
            ]),
            
            iaa.GaussianBlur((0, 3.0)),

            iaa.OneOf([    
                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=5),
                iaa.GaussianBlur(0.),
            ]),    
            iaa.Emboss(alpha=(0.2, 0.6), strength=(0.5, 1.1)),

            iaa.OneOf([    
                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=5),
                iaa.GaussianBlur(0.),
            ]), 

            # iaa.SomeOf([1,2], [
            iaa.OneOf([    
                iaa.LogContrast(gain=(0.6, 1.4)),
                iaa.GammaContrast((0.5, 2.0)),
                iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)),
                iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30)),
                iaa.MultiplyBrightness((0.5, 1.5)),
                # iaa.imgcorruptlike.Brightness(severity=2),
            ]), 


            # iaa.SomeOf([1,2], [
            iaa.OneOf([    
                # iaa.OneOf([
                #     iaa.MotionBlur(k=3),
                #     iaa.MotionBlur(k=5),
                #     # iaa.MotionBlur(k=7),

                #     iaa.GaussianBlur((0, 3.0)),
                #     # iaa.MedianBlur(k=(1,3)),
                #     # iaa.AverageBlur(k=(1,3)),

                # ]),
                iaa.OneOf([

                    # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                    # iaa.Flipud(1),
                    # iaa.Fliplr(1),
                    iaa.LinearContrast((0.8,1.1), per_channel=0.5),
                    iaa.Add((-5, 5), per_channel=0.5),
                ]),
                # iaa.OneOf([
                    # iaa.GaussianBlur((0, 3.0)),
                    # iaa.MedianBlur(k=(1,3)),
                    # iaa.AverageBlur(k=(1,3)),
                    # iaa.MotionBlur(k=3),
                #     iaa.MotionBlur(k=5),
                    # iaa.MotionBlur(k=5),
                    # iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.5, 1.0)),
                    # iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))
                # ]),
                iaa.OneOf(
                    [
                        
                        iaa.BlendAlphaSimplexNoise(iaa.OneOf([
                        #    iaa.EdgeDetect(alpha=(0.3, 0.7)),
                        iaa.DirectedEdgeDetect(alpha=(0.3, 0.5), direction=(0.0, 1.0))
                        ])),
                        # iaa.SimplexNoiseAlpha(
                        #     iaa.EdgeDetect(1.0),
                        #     sigmoid_thresh=iap.Normal(60.0, 30.0)
                        # ),
                        # iaa.FrequencyNoiseAlpha(
                        #     first=iaa.EdgeDetect(1.0),
                        #     upscale_method="linear",
                        #     exponent=-2,
                        #     sigmoid=False
                        # ),
                        # iaa.FrequencyNoiseAlpha(
                        #     first=iaa.EdgeDetect(1.0),
                        #     upscale_method="linear",
                        #     sigmoid_thresh=iap.Normal(10.0, 5.0)
                        # ),
                        
                    ],
                    # random_order=True
                ),
                # iaa.OneOf([
                #     iaa.MotionBlur(k=3),
                #     iaa.MotionBlur(k=5),
                #     # iaa.MotionBlur(k=7),
                #     # iaa.MotionBlur(k=9),
                #     # iaa.MotionBlur(k=11),
                #     # iaa.MotionBlur(k=13),
                #     # iaa.MotionBlur(k=15),
                #     # iaa.MotionBlur(k=17),
                # ]),
            ]),
        ],
        random_order=True
    )

    image_out_aug = seq(images=io_images)
    # pil_image = Image.fromarray(image_aug[0])
    cv_image = image_out_aug[0]
    return cv_image


def aug_image_for_plate_crnn(image):

    io_images = [image]

    seq = iaa.Sequential(
        [

            iaa.SomeOf([1,1], [

                iaa.MotionBlur(k=3),

                iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                iaa.LinearContrast((0.7,1.3), per_channel=True),
                iaa.Add((-15, 15), per_channel=False),
                iaa.MedianBlur(k=(1,3)),
                iaa.AverageBlur(k=(1,3)),
                iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.5, 1.0)),

                iaa.pillike.EnhanceSharpness(),
                iaa.pillike.EnhanceBrightness(),
                iaa.pillike.EnhanceContrast(),
            ]),
            # iaa.OneOf([
            #     iaa.pillike.EnhanceSharpness(),
            #     iaa.pillike.EnhanceBrightness(),
            #     iaa.pillike.EnhanceContrast(),
            # ])
            # iaa.OneOf(
            #     [
            #         iaa.SimplexNoiseAlpha(iaa.OneOf([
            #         #    iaa.EdgeDetect(alpha=(0.3, 0.7)),
            #            iaa.DirectedEdgeDetect(alpha=(0.3, 0.7), direction=(0.0, 1.0))
            #         ])),
            #         # iaa.SimplexNoiseAlpha(
            #         #     iaa.EdgeDetect(1.0),
            #         #     sigmoid_thresh=iap.Normal(60.0, 30.0)
            #         # ),
            #         # iaa.FrequencyNoiseAlpha(
            #         #     first=iaa.EdgeDetect(1.0),
            #         #     upscale_method="linear",
            #         #     exponent=-2,
            #         #     sigmoid=False
            #         # ),
            #         # iaa.FrequencyNoiseAlpha(
            #         #     first=iaa.EdgeDetect(1.0),
            #         #     upscale_method="linear",
            #         #     sigmoid_thresh=iap.Normal(10.0, 5.0)
            #         # ),
                    
            #     ],
            #     # random_order=True
            # ),

        ],
        random_order=False
    )

    image_aug = seq(images=io_images)
    # pil_image = Image.fromarray(image_aug[0])
    cv_image = image_aug[0]
    return cv_image


def aug_image_to_bigger(image,points):
    io_images = [image]
    polygons = []
    polygons.append([ ia.Polygon([(x,y) for x,y in points] )  ])

    seq = iaa.Sequential(
        [
            iaa.Resize(size=1.8),    
        ],
        random_order=True
    )

    image_aug, polygon_aug = seq(images=io_images, polygons=polygons)
    cv_image = image_aug[0]
    plg = polygon_aug[0][0]
    new_polygon = [[int(x),int(y)] for x,y in zip(plg.xx, plg.yy) ]
    return cv_image, np.array(new_polygon)


def aug_image_2(image):

    # new_image = image.convert('RGBA')
    # image_array = np.frombuffer(new_image.tobytes(), dtype=np.uint8)
    # image_array = image_array.reshape((new_image.size[1], new_image.size[0], 4))
    # image_array = image_array[:,:,:3]
    io_images = [image]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
            # iaa.Affine(rotate=(-5,5)),
            # iaa.OneOf([
            #     iaa.OneOf([
            #         iaa.Flipud(1),
            #         iaa.Fliplr(1)
            #     ]),
            #     iaa.SomeOf((1,2),[
            #             iaa.Flipud(1),
            #             iaa.Fliplr(1)
            #         ],
            #     random_order=True
            #     ),
            # ]),
            # iaa.Resize([0.8],1.4),
            iaa.OneOf([

                # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                # iaa.Flipud(1),
                # iaa.Fliplr(1),
                iaa.LinearContrast((0.7,1.3), per_channel=True),
                iaa.Add((-20, 20), per_channel=False),
            ]),
            iaa.OneOf([
                iaa.GaussianBlur((0, 3.0)),
                iaa.MedianBlur(k=(1,3)),
                iaa.AverageBlur(k=(1,3)),
                # iaa.MotionBlur(k=3),
                # iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.5, 1.0)),
                # iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))
            ]),
            # iaa.OneOf(
            #     [
                    
            #         iaa.SimplexNoiseAlpha(iaa.OneOf([
            #         #    iaa.EdgeDetect(alpha=(0.3, 0.7)),
            #            iaa.DirectedEdgeDetect(alpha=(0.3, 0.7), direction=(0.0, 1.0))
            #         ])),
            #         # iaa.SimplexNoiseAlpha(
            #         #     iaa.EdgeDetect(1.0),
            #         #     sigmoid_thresh=iap.Normal(60.0, 30.0)
            #         # ),
            #         # iaa.FrequencyNoiseAlpha(
            #         #     first=iaa.EdgeDetect(1.0),
            #         #     upscale_method="linear",
            #         #     exponent=-2,
            #         #     sigmoid=False
            #         # ),
            #         # iaa.FrequencyNoiseAlpha(
            #         #     first=iaa.EdgeDetect(1.0),
            #         #     upscale_method="linear",
            #         #     sigmoid_thresh=iap.Normal(10.0, 5.0)
            #         # ),
                    
            #     ],
            #     # random_order=True
            # ),
            # iaa.OneOf([
            #     iaa.MotionBlur(k=3),
            #     iaa.MotionBlur(k=5),
            #     iaa.MotionBlur(k=7),
            #     iaa.MotionBlur(k=9),
            #     iaa.MotionBlur(k=11),
            #     # iaa.MotionBlur(k=13),
            #     # iaa.MotionBlur(k=15),
            #     # iaa.MotionBlur(k=17),
            # ]),

        ],
        random_order=True
    )

    image_aug = seq(images=io_images)
    # pil_image = Image.fromarray(image_aug[0])
    cv_image = image_aug[0]
    return cv_image

def aug_image_dir_for_egg(data_dir):
    """
        整个文件夹的图像增强
    """
    io_images = []
    polygons = []

    for idx, image_name in enumerate(os.listdir(data_dir)):
        image_path = data_dir + os.sep + image_name
        img = imageio.imread(image_path)
        io_images.append(img)

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            iaa.Rot90([0,5], keep_size=False),
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
            iaa.Affine(
                rotate=[-15, 15],
                translate_px={"x":(1,35), "y":(1,15)}
            ),
            # sometimes([
            # iaa.PerspectiveTransform(scale=(0.01, 0.15),keep_size=False),
            # ]),
            # iaa.OneOf(
            #     [
            #         iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                    iaa.Fliplr(0.5),

                    iaa.Flipud(0.5),
                    iaa.LinearContrast((0.6,1.4), per_channel=False),
                    iaa.Add((-10, 10), per_channel=False),
            #         #iaa.SimplexNoiseAlpha(iaa.OneOf([
            #         #    iaa.EdgeDetect(alpha=(0.5, 1.0)),
            #         #    iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0))
            #         #])),

            #     ]
            # ),
            # iaa.OneOf([
            #     iaa.GaussianBlur((0, 3.0)),
            #     iaa.MedianBlur(k=(3,5)),
            #     iaa.AverageBlur(k=(3,5))
            # ])
        ],
        random_order=True
    )

    # image_aug, polygon_aug = seq(images=io_images, polygons=polygons)
    image_aug = seq(images=io_images)
    return image_aug

    # ia.imshow(new_image)
    

def aug_image_for_dir(data_dir):
    """
        整个文件夹的图像增强
    """
    io_images = []
    polygons = []

    for idx, image_name in enumerate(os.listdir(data_dir)):
        image_path = data_dir + os.sep + image_name
        img = imageio.imread(image_path)
        io_images.append(img)

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            iaa.Rot90([0,5], keep_size=False),
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
            iaa.Affine(translate_px={"x":(1,35), "y":(1,15)}),
            # sometimes([
            # iaa.PerspectiveTransform(scale=(0.01, 0.15),keep_size=False),
            # ]),
            # iaa.OneOf(
            #     [
            #         iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                    iaa.Fliplr(0.3),

                    iaa.Flipud(0.3),
                    iaa.LinearContrast((0.5,1.5), per_channel=False),
                    iaa.Add((-10, 10), per_channel=False),
            #         #iaa.SimplexNoiseAlpha(iaa.OneOf([
            #         #    iaa.EdgeDetect(alpha=(0.5, 1.0)),
            #         #    iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0))
            #         #])),

            #     ]
            # ),
            # iaa.OneOf([
            #     iaa.GaussianBlur((0, 3.0)),
            #     iaa.MedianBlur(k=(3,5)),
            #     iaa.AverageBlur(k=(3,5))
            # ])
        ],
        random_order=True
    )

    # image_aug, polygon_aug = seq(images=io_images, polygons=polygons)
    image_aug = seq(images=io_images)
    return image_aug

    # ia.imshow(new_image)
    
def aug_image_for_electric_meter(image):

    new_image = image.convert('RGBA')
    image_array = np.frombuffer(new_image.tobytes(), dtype=np.uint8)
    image_array = image_array.reshape((new_image.size[1], new_image.size[0], 4))
    image_array = image_array[:,:,:3]
    io_images = [image_array]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            iaa.OneOf([
                # iaa.GaussianBlur((0, 3.0)),
                # iaa.MedianBlur(k=(1,3)),
                # iaa.AverageBlur(k=(1,3)),
                # iaa.MotionBlur(k=5),
                # iaa.MotionBlur(k=7),

                # iaa.pillike.EnhanceSharpness(),
                # iaa.pillike.EnhanceBrightness(),
                # iaa.pillike.EnhanceContrast(),
                # iaa.pillike.EnhanceColor(),

                iaa.imgcorruptlike.DefocusBlur(severity=3),
                iaa.imgcorruptlike.DefocusBlur(severity=2),
                iaa.imgcorruptlike.DefocusBlur(severity=2),
            
                # iaa.imgcorruptlike.ZoomBlur(severity=1),
                iaa.imgcorruptlike.Fog(severity=1),
                # iaa.imgcorruptlike.Frost(severity=1),
                iaa.imgcorruptlike.Snow(severity=1),
                iaa.imgcorruptlike.Spatter(severity=1),
                iaa.imgcorruptlike.JpegCompression(severity=2),
                iaa.imgcorruptlike.JpegCompression(severity=2),
                iaa.pillike.FilterSmooth(),
                iaa.pillike.FilterSmoothMore(),
                iaa.AveragePooling(2),
                iaa.AveragePooling(1),

            ]),
            # iaa.OneOf( [

            #     iaa.BlendAlphaSimplexNoise(
            #         iaa.Multiply(iap.Choice([0.7, 1.3]), per_channel=True)
            #     ),

            #     iaa.BlendAlphaSimplexNoise(iaa.OneOf([
            #         iaa.EdgeDetect(alpha=(0.3, 0.7)),
            #         # iaa.DirectedEdgeDetect(alpha=(0.2, 0.6), direction=(0.0, 1.0))
            #         ])
            #     ),
            #     iaa.imgcorruptlike.JpegCompression(severity=2),
            #     iaa.imgcorruptlike.JpegCompression(severity=2),



            # ]),
            iaa.OneOf([
                    iaa.MotionBlur(k=11),
                    iaa.MotionBlur(k=9),
                    iaa.GaussianBlur((0, 5.0)),
                    iaa.MedianBlur(k=(1,5)),
                    iaa.AverageBlur(k=(1,5)),


                iaa.imgcorruptlike.JpegCompression(severity=2),
                iaa.imgcorruptlike.JpegCompression(severity=2),

                    iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.7, 1.0)),

                    iaa.BlendAlpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()),
                    iaa.Add((0, 10), per_channel=False),
                    

            ]),

            iaa.OneOf([
                iaa.MotionBlur(k=9),
                iaa.MotionBlur(k=11),

                iaa.GaussianBlur((0, 5.0)),

                iaa.imgcorruptlike.JpegCompression(severity=2),
                iaa.imgcorruptlike.JpegCompression(severity=2),

                iaa.MedianBlur(k=(1,5)),
                iaa.AverageBlur(k=(1,5)),
                iaa.MotionBlur(k=9),

            ])


        ],
        random_order=True
    )

    image_aug = seq(images=io_images)
    pil_image = Image.fromarray(image_aug[0])
    return pil_image

def aug_image(image):

    new_image = image.convert('RGBA')
    image_array = np.frombuffer(new_image.tobytes(), dtype=np.uint8)
    image_array = image_array.reshape((new_image.size[1], new_image.size[0], 4))
    image_array = image_array[:,:,:3]
    io_images = [image_array]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [
            iaa.MedianBlur(k=(1,3)),
            iaa.MotionBlur(k=5),
            iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.7, 1.0)),
            iaa.GaussianBlur((0, 3.0)),
            iaa.GammaContrast((0.8, 1.2)),
            iaa.MotionBlur(k=3),
            # iaa.SomeOf([1,3], [


            #         iaa.OneOf([
            #             iaa.OneOf([
            #                 iaa.MotionBlur(k=7),
            #                 iaa.GaussianBlur((0, 5.0)),
            #             ]),
            #             iaa.SomeOf([0,2], [
            #                 iaa.MedianBlur(k=(1,3)),
            #                 iaa.AverageBlur(k=(1,3)),
            #                 iaa.GaussianBlur((0, 3.0)),
            #                 iaa.MotionBlur(k=3),
            #                 iaa.MotionBlur(k=5),

            #                 iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.7, 1.0)),
            #                 iaa.Emboss(alpha=(0.2, 0.8), strength=(0.7, 1.1)),

            #             ]), 
            #         ]),


            #         # iaa.MotionBlur(k=9),
            #         # iaa.MotionBlur(k=11),
            #         # iaa.MotionBlur(k=13),
            #         # iaa.MotionBlur(k=15),
            #         # iaa.MotionBlur(k=17),

            #     iaa.OneOf([


            #         iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()),
            #         iaa.LogContrast(gain=(0.8, 1.2)),
            #         iaa.GammaContrast((0.8, 1.2)),
            #         iaa.Add((0, 10), per_channel=False),
            #     ]),

            #     ],
            # ),
        ],
        random_order=False
    )
    # seq = iaa.Sequential(
    #     [

    #         iaa.SomeOf([1,2], [
    #             iaa.OneOf([
    #                 # iaa.GaussianBlur((0, 3.0)),
    #                 # iaa.MedianBlur(k=(1,3)),
    #                 # iaa.AverageBlur(k=(1,3)),
    #                 # iaa.MotionBlur(k=5),
    #                 # iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.5, 1.0)),
    #                 # iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))
    #             ]),
    #             iaa.SimplexNoiseAlpha(
    #                 first=iaa.Multiply(iap.Choice([0.7, 1.3]), per_channel=True)
    #             ),
    #             iaa.OneOf(
    #                 [
                        
    #                     iaa.SimplexNoiseAlpha(iaa.OneOf([
    #                     #    iaa.EdgeDetect(alpha=(0.3, 0.7)),
    #                     iaa.DirectedEdgeDetect(alpha=(0.2, 0.6), direction=(0.0, 1.0))
    #                     ])),
    #                     # iaa.SimplexNoiseAlpha(
    #                     #     iaa.EdgeDetect(1.0),
    #                     #     sigmoid_thresh=iap.Normal(60.0, 30.0)
    #                     # ),
    #                     # iaa.FrequencyNoiseAlpha(
    #                     #     first=iaa.EdgeDetect(0.6),
    #                     #     upscale_method="linear",
    #                     #     exponent=-2,
    #                     #     sigmoid=False
    #                     # ),
    #                     # iaa.FrequencyNoiseAlpha(
    #                     #     first=iaa.EdgeDetect(1.0),
    #                     #     upscale_method="linear",
    #                     #     sigmoid_thresh=iap.Normal(10.0, 5.0)
    #                     # ),
                        
    #                 ],
    #                 # random_order=True
    #             ),

    #             ]
    #         ),
    #         iaa.SomeOf([1,2], [
    #             iaa.OneOf([
    #                 iaa.MotionBlur(k=3),
    #                 iaa.MotionBlur(k=5),
    #                 iaa.GaussianBlur((0, 3.0)),
    #                 iaa.MedianBlur(k=(1,3)),
    #                 iaa.AverageBlur(k=(1,3)),

    #                 # iaa.MotionBlur(k=7),
    #                 # iaa.MotionBlur(k=9),
    #                 # iaa.MotionBlur(k=11),
    #                 # iaa.MotionBlur(k=13),
    #                 # iaa.MotionBlur(k=15),
    #                 # iaa.MotionBlur(k=17),
    #             ]),
    #             iaa.OneOf([
    #                 iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()),
    #                 iaa.LogContrast(gain=(0.8, 1.2)),
    #                 iaa.GammaContrast((0.8, 1.2))
    #             ]),
    #             iaa.OneOf([

    #                 # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
    #                 # iaa.Flipud(1),
    #                 # iaa.Fliplr(1),
    #                 # iaa.LinearContrast((0.7,1.3), per_channel=False),

    #                 # shuxian1
    #                 iaa.LinearContrast((0.8,1.3), per_channel=False),
    #                 iaa.Add((0, 10), per_channel=False),

    #                 # shuxian2
    #                 # iaa.Add((-10, 0), per_channel=False),
    #             ]),
    #             ],
    #         ),
    #     ],
    #     random_order=True
    # )

    image_aug = seq(images=io_images)
    pil_image = Image.fromarray(image_aug[0])
    return pil_image


def aug_image_cn(image):

    new_image = image.convert('RGBA')
    image_array = np.frombuffer(new_image.tobytes(), dtype=np.uint8)
    image_array = image_array.reshape((new_image.size[1], new_image.size[0], 4))
    image_array = image_array[:,:,:3]
    io_images = [image_array]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
            # iaa.Affine(translate_px={"x":(1,5)}),
            # iaa.OneOf([
            #     iaa.OneOf([
            #         iaa.Flipud(1),
            #         iaa.Fliplr(1)
            #     ]),
            #     iaa.SomeOf((1,2),[
            #             iaa.Flipud(1),
            #             iaa.Fliplr(1)
            #         ],
            #     random_order=True
            #     ),
            # ]),
            iaa.OneOf([
                # iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()),
                iaa.LogContrast(gain=(0.8, 1.3)),
                iaa.GammaContrast((0.8, 1.3))
            ]),
            iaa.OneOf([

                # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                # iaa.Flipud(1),
                # iaa.Fliplr(1),
                # iaa.LinearContrast((0.7,1.3), per_channel=False),

                # shuxian1
                iaa.LinearContrast((0.9,1.3), per_channel=False),
                iaa.Add((-7, 7), per_channel=False),

                # shuxian2
                # iaa.Add((-10, 0), per_channel=False),
            ]),
            iaa.OneOf([
                iaa.MotionBlur(k=3),
                iaa.MotionBlur(k=5),
                iaa.GaussianBlur((0, 3.0)),
                iaa.MedianBlur(k=(1,3)),
                iaa.AverageBlur(k=(1,3)),
                # iaa.MotionBlur(k=7),
                # iaa.MotionBlur(k=9),
                # iaa.MotionBlur(k=11),
                # iaa.MotionBlur(k=13),
                # iaa.MotionBlur(k=15),
                # iaa.MotionBlur(k=17),
            ]),
            # iaa.SomeOf([1,2],
            #     [
            #     # iaa.OneOf([
            #         # iaa.GaussianBlur((0, 3.0)),
            #         # iaa.MedianBlur(k=(1,3)),
            #         # iaa.AverageBlur(k=(1,3)),
            #         # iaa.MotionBlur(k=5),
            #         # iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.5, 1.0)),
            #         # iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))
            #     # ]),
            #     iaa.SimplexNoiseAlpha(
            #         first=iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True)
            #     ),
            #     iaa.OneOf(
            #         [
                        
            #             iaa.SimplexNoiseAlpha(iaa.OneOf([
            #             #    iaa.EdgeDetect(alpha=(0.3, 0.7)),
            #             iaa.DirectedEdgeDetect(alpha=(0.2, 0.6), direction=(0.0, 1.0))
            #             ])),
            #             # iaa.SimplexNoiseAlpha(
            #             #     iaa.EdgeDetect(1.0),
            #             #     sigmoid_thresh=iap.Normal(60.0, 30.0)
            #             # ),
            #             iaa.FrequencyNoiseAlpha(
            #                 first=iaa.EdgeDetect(0.6),
            #                 upscale_method="linear",
            #                 exponent=-2,
            #                 sigmoid=False
            #             ),
            #             # iaa.FrequencyNoiseAlpha(
            #             #     first=iaa.EdgeDetect(1.0),
            #             #     upscale_method="linear",
            #             #     sigmoid_thresh=iap.Normal(10.0, 5.0)
            #             # ),
                        
            #         ],
            #         # random_order=True
            #     ),

            #     ]
            # ),

        ],
        random_order=True
    )

    image_aug = seq(images=io_images)
    pil_image = Image.fromarray(image_aug[0])
    return pil_image


def aug_image_shuxian(image):

    new_image = image.convert('RGBA')
    image_array = np.frombuffer(new_image.tobytes(), dtype=np.uint8)
    image_array = image_array.reshape((new_image.size[1], new_image.size[0], 4))
    image_array = image_array[:,:,:3]
    io_images = [image_array]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
            # iaa.Affine(translate_px={"x":(1,5)}),
            # iaa.OneOf([
            #     iaa.OneOf([
            #         iaa.Flipud(1),
            #         iaa.Fliplr(1)
            #     ]),
            #     iaa.SomeOf((1,2),[
            #             iaa.Flipud(1),
            #             iaa.Fliplr(1)
            #         ],
            #     random_order=True
            #     ),
            # ]),
            # iaa.JpegCompression(compression=(60, 95)),
            # iaa.OneOf([
            #     # iaa.Affine(translate_px={"x":(1,5)}),
            #     # iaa.Affine(
            #     #     scale={"x": (0.8, 1.1), "y": (0.8, 1.1)}, # scale images to 80-120% of their size, individually per axis
            #     #     # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
            #     #     rotate=(-15, 15), # rotate by -45 to +45 degrees
            #     #     # shear=(-16, 16), # shear by -16 to +16 degrees
            #     #     # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
            #     #     # cval=(0, 255), # if mode is constant, use a cval between 0 and 255
            #     #     # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
            #     # ),
            #     iaa.PerspectiveTransform(scale=(0.01, 0.1), keep_size=True),
            # ]),
            iaa.SomeOf([1,4], 
            [
                iaa.OneOf([

                    # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                    # iaa.Flipud(1),
                    # iaa.Fliplr(1),
                    # iaa.LinearContrast((0.7,1.3), per_channel=False),

                    # shuxian1
                    # iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()),
                    # iaa.LogContrast(gain=(0.8, 1.3)),
                    # iaa.GammaContrast((0.8, 1.5)),
                    iaa.LinearContrast((0.9,1.5), per_channel=False),
                    iaa.Add((-5, 10), per_channel=False),

                    # shuxian2
                    # iaa.Add((-10, 0), per_channel=False),
                ]),
                # iaa.SomeOf([0,1],
                #     [
                    iaa.OneOf([
                        iaa.GaussianBlur((0, 3.0)),
                        iaa.MedianBlur(k=(1,3)),
                        iaa.AverageBlur(k=(1,3)),
                        # iaa.MotionBlur(k=5),
                        # iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.5, 1.0)),
                        # iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))
                    ]),
                    # iaa.SimplexNoiseAlpha(
                    #     first=iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True)
                    # ),
                    iaa.OneOf(
                        [
                            iaa.Dropout(p=(0.01, 0.05), per_channel=0.5),
                            iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()),
                            iaa.LogContrast(gain=(0.8, 1.3)),
                            iaa.GammaContrast((0.8, 1.5)),

                            iaa.SimplexNoiseAlpha(iaa.OneOf([
                            #    iaa.EdgeDetect(alpha=(0.3, 0.7)),
                            iaa.DirectedEdgeDetect(alpha=(0.2, 0.6), direction=(0.0, 1.0))
                            ])),
                            # iaa.SimplexNoiseAlpha(
                            #     iaa.EdgeDetect(1.0),
                            #     sigmoid_thresh=iap.Normal(60.0, 30.0)
                            # ),
                            iaa.FrequencyNoiseAlpha(
                                first=iaa.EdgeDetect(0.6),
                                upscale_method="linear",
                                exponent=-2,
                                sigmoid=False
                            ),
                            # iaa.FrequencyNoiseAlpha(
                            #     first=iaa.EdgeDetect(1.0),
                            #     upscale_method="linear",
                            #     sigmoid_thresh=iap.Normal(10.0, 5.0)
                            # ),
                            
                        ],
                        # random_order=True
                    ),

                    # ]
                # ),
                iaa.OneOf([
                    iaa.MotionBlur(k=3),
                    iaa.MotionBlur(k=5),
                    iaa.MotionBlur(k=7),
                    iaa.MotionBlur(k=9),
                    # iaa.MotionBlur(k=11),
                    # iaa.MotionBlur(k=13),
                    # iaa.MotionBlur(k=15),
                    # iaa.MotionBlur(k=17),
                ]),

            ], random_order=True),
        ],
        random_order=True
    )

    image_aug = seq(images=io_images)
    pil_image = Image.fromarray(image_aug[0])
    return pil_image

def aug_image_shuxian_2(image):

    new_image = image.convert('RGBA')
    image_array = np.frombuffer(new_image.tobytes(), dtype=np.uint8)
    image_array = image_array.reshape((new_image.size[1], new_image.size[0], 4))
    image_array = image_array[:,:,:3]
    io_images = [image_array]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
            # iaa.Affine(translate_px={"x":(1,5)}),
            # iaa.OneOf([
            #     iaa.OneOf([
            #         iaa.Flipud(1),
            #         iaa.Fliplr(1)
            #     ]),
            #     iaa.SomeOf((1,2),[
            #             iaa.Flipud(1),
            #             iaa.Fliplr(1)
            #         ],
            #     random_order=True
            #     ),
            # ]),
            # iaa.JpegCompression(compression=(60, 95)),
            # iaa.OneOf([
            #     # iaa.Affine(translate_px={"x":(1,5)}),
            #     # iaa.Affine(
            #     #     scale={"x": (0.8, 1.1), "y": (0.8, 1.1)}, # scale images to 80-120% of their size, individually per axis
            #     #     # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
            #     #     rotate=(-15, 15), # rotate by -45 to +45 degrees
            #     #     # shear=(-16, 16), # shear by -16 to +16 degrees
            #     #     # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
            #     #     # cval=(0, 255), # if mode is constant, use a cval between 0 and 255
            #     #     # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
            #     # ),
            #     iaa.PerspectiveTransform(scale=(0.01, 0.1), keep_size=True),
            # ]),
            iaa.SomeOf([1,4], 
            [
                iaa.OneOf([

                    # iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                    # iaa.Flipud(1),
                    # iaa.Fliplr(1),
                    # iaa.LinearContrast((0.7,1.3), per_channel=False),

                    # shuxian1
                    # iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()),
                    # iaa.LogContrast(gain=(0.8, 1.3)),
                    # iaa.GammaContrast((0.8, 1.5)),
                    iaa.LinearContrast((0.9,1.5), per_channel=False),
                    iaa.Add((-5, 10), per_channel=False),

                    # shuxian2
                    # iaa.Add((-10, 0), per_channel=False),
                ]),
                # iaa.SomeOf([0,1],
                #     [
                iaa.OneOf([
                    iaa.GaussianBlur((0, 5.0)),
                    iaa.MedianBlur(k=(1,5)),
                    iaa.AverageBlur(k=(1,5)),
                    # iaa.MotionBlur(k=5),
                    # iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.5, 1.0)),
                    # iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))
                ]),
                # iaa.SimplexNoiseAlpha(
                #     first=iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True)
                # ),
                iaa.OneOf(
                    [
                        iaa.Dropout(p=(0.01, 0.05), per_channel=0.5),
                        iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()),
                        iaa.LogContrast(gain=(0.8, 1.3)),
                        iaa.GammaContrast((0.8, 1.5)),

                        # iaa.SimplexNoiseAlpha(iaa.OneOf([
                        # #    iaa.EdgeDetect(alpha=(0.3, 0.7)),
                        # iaa.DirectedEdgeDetect(alpha=(0.2, 0.6), direction=(0.0, 1.0))
                        # ])),
                        # iaa.SimplexNoiseAlpha(
                        #     iaa.EdgeDetect(1.0),
                        #     sigmoid_thresh=iap.Normal(60.0, 30.0)
                        # ),
                        # iaa.FrequencyNoiseAlpha(
                        #     first=iaa.EdgeDetect(0.6),
                        #     upscale_method="linear",
                        #     exponent=-2,
                        #     sigmoid=False
                        # ),
                        # iaa.FrequencyNoiseAlpha(
                        #     first=iaa.EdgeDetect(1.0),
                        #     upscale_method="linear",
                        #     sigmoid_thresh=iap.Normal(10.0, 5.0)
                        # ),
                        
                    ],
                    # random_order=True
                ),

                    # ]
                # ),
                iaa.OneOf([
                    iaa.MotionBlur(k=3),
                    iaa.MotionBlur(k=5),
                    iaa.MotionBlur(k=7),
                    iaa.MotionBlur(k=9),
                    # iaa.MotionBlur(k=11),
                    # iaa.MotionBlur(k=13),
                    # iaa.MotionBlur(k=15),
                    # iaa.MotionBlur(k=17),
                ]),

            ], random_order=True),
        ],
        random_order=True
    )

    image_aug = seq(images=io_images)
    pil_image = Image.fromarray(image_aug[0])
    return pil_image


def aug_image_shuxian_3(image):

    new_image = image.convert('RGBA')
    image_array = np.frombuffer(new_image.tobytes(), dtype=np.uint8)
    image_array = image_array.reshape((new_image.size[1], new_image.size[0], 4))
    image_array = image_array[:,:,:3]
    io_images = [image_array]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [
            # iaa.AdditiveGaussianNoise(scale=(0.01 * 255)),
            # iaa.Affine(translate_px={"x":(1,5)}),
            # iaa.OneOf([
            #     iaa.OneOf([
            #         iaa.Flipud(1),
            #         iaa.Fliplr(1)
            #     ]),
            #     iaa.SomeOf((1,2),[
            #             iaa.Flipud(1),
            #             iaa.Fliplr(1)
            #         ],
            #     random_order=True
            #     ),
            # ]),
            
            # iaa.OneOf([
            #     # iaa.Affine(translate_px={"x":(1,5)}),
            #     # iaa.Affine(
            #     #     scale={"x": (0.8, 1.1), "y": (0.8, 1.1)}, # scale images to 80-120% of their size, individually per axis
            #     #     # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
            #     #     rotate=(-15, 15), # rotate by -45 to +45 degrees
            #     #     # shear=(-16, 16), # shear by -16 to +16 degrees
            #     #     # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
            #     #     # cval=(0, 255), # if mode is constant, use a cval between 0 and 255
            #     #     # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
            #     # ),
            #     iaa.PerspectiveTransform(scale=(0.01, 0.1), keep_size=True),
            # ]),

            iaa.OneOf([

                iaa.Dropout(p=(0.01, 0.1), per_channel=0.5),
                # iaa.Flipud(1),
                # iaa.Fliplr(1),
                # iaa.LinearContrast((0.7,1.3), per_channel=False),

                # shuxian1
                # iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()),
                # iaa.LogContrast(gain=(0.8, 1.3)),
                # iaa.GammaContrast((0.8, 1.5)),
                # iaa.LinearContrast((0.9,1.5), per_channel=False),
                iaa.Add((0, 10), per_channel=False),

                # shuxian2
                # iaa.Add((-10, 0), per_channel=False),
            ]),
            # iaa.SomeOf([0,1],
            #     [
                iaa.OneOf([
                    iaa.GaussianBlur((0, 3.0)),
                    iaa.MedianBlur(k=(1,3)),
                    iaa.AverageBlur(k=(1,3)),
                    iaa.MotionBlur(k=3),
                    iaa.MotionBlur(k=5),
                    iaa.MotionBlur(k=5),
                    # iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.5, 1.0)),
                    # iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))
                ]),
                # iaa.SimplexNoiseAlpha(
                #     first=iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True)
                # ),
                # iaa.OneOf(
                #     [
                        
                #         iaa.SimplexNoiseAlpha(iaa.OneOf([
                #         #    iaa.EdgeDetect(alpha=(0.3, 0.7)),
                #         iaa.DirectedEdgeDetect(alpha=(0.2, 0.6), direction=(0.0, 1.0))
                #         ])),
                #         # iaa.SimplexNoiseAlpha(
                #         #     iaa.EdgeDetect(1.0),
                #         #     sigmoid_thresh=iap.Normal(60.0, 30.0)
                #         # ),
                #         iaa.FrequencyNoiseAlpha(
                #             first=iaa.EdgeDetect(0.6),
                #             upscale_method="linear",
                #             exponent=-2,
                #             sigmoid=False
                #         ),
                #         # iaa.FrequencyNoiseAlpha(
                #         #     first=iaa.EdgeDetect(1.0),
                #         #     upscale_method="linear",
                #         #     sigmoid_thresh=iap.Normal(10.0, 5.0)
                #         # ),
                        
                #     ],
                #     # random_order=True
                # ),

                # ]
            # ),
            # iaa.OneOf([
            #     iaa.MotionBlur(k=3),
            #     # iaa.MotionBlur(k=5),
            #     # iaa.MotionBlur(k=7),
            #     # iaa.MotionBlur(k=9),
            #     # iaa.MotionBlur(k=11),
            #     # iaa.MotionBlur(k=13),
            #     # iaa.MotionBlur(k=15),
            #     # iaa.MotionBlur(k=17),
            # ]),
            # iaa.JpegCompression(compression=(85, 95)),
        ],
        random_order=False
    )

    image_aug = seq(images=io_images)
    pil_image = Image.fromarray(image_aug[0])
    return pil_image

def aug_image_warp_perspective(image):

    paste_image = Image.new('RGBA', (image.size[0] + 200, image.size[1] + 200), (0, 0, 0, 0))
    paste_image.paste(image, (100,100))

    image_array = np.frombuffer(paste_image.tobytes(), dtype=np.uint8)
    image_array = image_array.reshape((paste_image.size[1], paste_image.size[0], 4))
    image_array = image_array[:,:,:3]

    alpha = paste_image.split()[-1]
    alpha = np.array(alpha, dtype=np.uint8)
    alpha = alpha[:, :, np.newaxis]
    alpha = np.concatenate((alpha, alpha, alpha), axis=2)
    io_images = [image_array, alpha]

    polygons = [[ ia.BoundingBox(   x1=100, 
                                        y1=100, 
                                        x2=image.size[0] + 100, 
                                        y2=image.size[1] + 100) 
                                         ]] * 2

    seq = iaa.Sequential(
        [
            iaa.PerspectiveTransform(scale=(0.01, 0.1), keep_size=False),
        ],
        random_order=True
    )

    image_aug, bbs_aug = seq(images=io_images, bounding_boxes=polygons)
    cv_image, bin_img = image_aug
    bbs = [[int(p.x1),int(p.y1), int(p.x2), int(p.y2)] for p in bbs_aug[0]]

    cv_image = copy.copy(cv_image[bbs[0][1]: bbs[0][3], bbs[0][0]: bbs[0][2], :])
    bin_img = copy.copy(bin_img[bbs[0][1]: bbs[0][3], bbs[0][0]: bbs[0][2], :])
 
    gray_img = cv2.cvtColor(bin_img, cv2.COLOR_RGB2GRAY)
    bin_img = cv2.threshold(gray_img, 5, 255, cv2.THRESH_BINARY)[1]
    # k = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
    # bin_img = cv2.erode(bin_img, k)

    cv2.imwrite("a.jpg", cv_image)
    cv2.imwrite("b.jpg", bin_img)

    bin_img = bin_img[:, :, np.newaxis]

    final_image = np.concatenate((cv_image,bin_img ), axis=2)
    pil_image = Image.fromarray(final_image)
    return pil_image

def aug_image_driving_license(image):

    new_image = image.convert('RGBA')
    image_array = np.frombuffer(new_image.tobytes(), dtype=np.uint8)
    image_array = image_array.reshape((new_image.size[1], new_image.size[0], 4))
    image_array = image_array[:,:,:3]
    io_images = [image_array]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [


            iaa.SomeOf([1,3],[
                iaa.OneOf([
                    
                    iaa.GaussianBlur((0, 3.0)),
                    iaa.MotionBlur(k=3),
                    iaa.MotionBlur(k=5),

                ]),

                iaa.OneOf([

                    iaa.MedianBlur(k=(1,3)),
                    iaa.AverageBlur(k=(1,3)),

                ]),
                iaa.OneOf([
                    iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.7, 1.0)),
                    # iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))

                ]),


                # iaa.JpegCompression(compression=(70, 95)),
                iaa.Add((-4, 4), per_channel=0.5),

                iaa.OneOf([

                    # iaa.JpegCompression(compression=(70, 74)),

                    iaa.GammaContrast((0.9, 1.1)),
                    iaa.LinearContrast((0.9,1.1), per_channel=0.5),
                    iaa.LogContrast(gain=(0.9, 1.1)),
                ]),


            ],
            random_order=True
        ),




        ],
        random_order=True
    )

    image_aug = seq(images=io_images)
    pil_image = Image.fromarray(image_aug[0])
    return pil_image




def aug_image_beijing_handwriting(image):

    new_image = image.convert('RGBA')
    image_array = np.frombuffer(new_image.tobytes(), dtype=np.uint8)
    image_array = image_array.reshape((new_image.size[1], new_image.size[0], 4))
    image_array = image_array[:,:,:3]
    io_images = [image_array]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [


            iaa.SomeOf([1,3],[
                iaa.OneOf([
                    
                    iaa.GaussianBlur((0, 3.0)),
                    iaa.MotionBlur(k=3),
                    iaa.MotionBlur(k=5),

                ]),

                iaa.OneOf([

                    iaa.MedianBlur(k=(1,3)),
                    iaa.AverageBlur(k=(1,3)),

                ]),
                iaa.OneOf([
                    iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.7, 1.0)),
                    iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1))

                ]),


                iaa.JpegCompression(compression=(70, 95)),
                iaa.Add((-4, 4), per_channel=0.5),

                iaa.OneOf([

                    # iaa.JpegCompression(compression=(70, 74)),

                    iaa.GammaContrast((0.9, 1.1)),
                    iaa.LinearContrast((0.9,1.1), per_channel=0.5),
                    iaa.LogContrast(gain=(0.9, 1.1)),
                ]),


            ],
            random_order=True
        ),



        ],
        random_order=True
    )

    image_aug = seq(images=io_images)
    pil_image = Image.fromarray(image_aug[0])
    return pil_image


def aug_image_boeing_black(image):

    new_image = image.convert('RGBA')
    image_array = np.frombuffer(new_image.tobytes(), dtype=np.uint8)
    image_array = image_array.reshape((new_image.size[1], new_image.size[0], 4))
    image_array = image_array[:,:,:3]
    io_images = [image_array]

    seq = iaa.Sequential(
        [
                iaa.OneOf([
                    iaa.GaussianBlur((0, 3.0)),

                    iaa.imgcorruptlike.DefocusBlur(severity=2),
                    iaa.imgcorruptlike.DefocusBlur(severity=2),
                    # iaa.imgcorruptlike.Fog(severity=2),
                    # iaa.imgcorruptlike.Frost(severity=2),
                    # iaa.imgcorruptlike.Snow(severity=2),
                    # iaa.imgcorruptlike.Spatter(severity=2),
                    # iaa.imgcorruptlike.Contrast(severity=2),
                    iaa.imgcorruptlike.JpegCompression(severity=2),
                    iaa.imgcorruptlike.Pixelate(severity=2),
                    # iaa.pillike.FilterBlur(),
                    # iaa.pillike.FilterSmooth(),
                    # iaa.pillike.FilterSmoothMore(),
                    iaa.imgcorruptlike.DefocusBlur(severity=2),
                    iaa.imgcorruptlike.DefocusBlur(severity=2),
                    iaa.imgcorruptlike.JpegCompression(severity=2),
                    iaa.imgcorruptlike.Pixelate(severity=2),

                    iaa.pillike.FilterBlur(),
                    iaa.pillike.FilterSmooth(),
                    iaa.pillike.FilterSmoothMore(),

                ]),

                iaa.OneOf([
                    iaa.MotionBlur(k=3),
                    iaa.MotionBlur(k=3),
                    iaa.MedianBlur(k=3),
                    iaa.AverageBlur(k=3),

                ]),

                iaa.Add((-8, 8), per_channel=1),
                iaa.LinearContrast((0.9,1.1), per_channel=0.5),

                iaa.OneOf([
                    iaa.imgcorruptlike.DefocusBlur(severity=2),
                    iaa.imgcorruptlike.DefocusBlur(severity=2),
                    # iaa.imgcorruptlike.Fog(severity=2),
                    # iaa.imgcorruptlike.Frost(severity=2),
                    # iaa.imgcorruptlike.Snow(severity=2),
                    # iaa.imgcorruptlike.Spatter(severity=2),
                    # iaa.imgcorruptlike.Contrast(severity=2),
                    iaa.imgcorruptlike.JpegCompression(severity=2),
                    iaa.imgcorruptlike.Pixelate(severity=2),
                    # iaa.pillike.FilterBlur(),
                    # iaa.pillike.FilterSmooth(),
                    # iaa.pillike.FilterSmoothMore(),
                    iaa.imgcorruptlike.DefocusBlur(severity=2),
                    iaa.imgcorruptlike.DefocusBlur(severity=2),
                    iaa.imgcorruptlike.JpegCompression(severity=2),
                    iaa.imgcorruptlike.Pixelate(severity=2),

                    iaa.pillike.FilterBlur(),
                    iaa.pillike.FilterSmooth(),
                    iaa.pillike.FilterSmoothMore(),
                    
                ]),

        ],
        random_order=False
    )

    image_aug = seq(images=io_images)
    pil_image = Image.fromarray(image_aug[0])
    return pil_image

def aug_image_boeing_white(image):

    new_image = image.convert('RGBA')
    image_array = np.frombuffer(new_image.tobytes(), dtype=np.uint8)
    image_array = image_array.reshape((new_image.size[1], new_image.size[0], 4))
    image_array = image_array[:,:,:3]
    io_images = [image_array]

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [
                iaa.OneOf([
                    iaa.GaussianBlur((0, 3.0)),

                    iaa.imgcorruptlike.DefocusBlur(severity=2),
                    iaa.imgcorruptlike.DefocusBlur(severity=2),
                    # iaa.imgcorruptlike.Fog(severity=1),
                    # iaa.imgcorruptlike.Frost(severity=1),
                    # iaa.imgcorruptlike.Snow(severity=1),
                    # iaa.imgcorruptlike.Spatter(severity=1),
                    # iaa.imgcorruptlike.Contrast(severity=1),
                    iaa.imgcorruptlike.JpegCompression(severity=2),
                    iaa.imgcorruptlike.Pixelate(severity=2),
                    # iaa.pillike.FilterBlur(),
                    # iaa.pillike.FilterSmooth(),
                    # iaa.pillike.FilterSmoothMore(),
                    iaa.imgcorruptlike.DefocusBlur(severity=2),
                    iaa.imgcorruptlike.DefocusBlur(severity=2),
                    iaa.imgcorruptlike.JpegCompression(severity=2),
                    iaa.imgcorruptlike.Pixelate(severity=2),

                    iaa.pillike.FilterBlur(),
                    iaa.pillike.FilterSmooth(),
                    iaa.pillike.FilterSmoothMore(),

                ]),

                iaa.OneOf([
                    iaa.MotionBlur(k=3),
                    iaa.MotionBlur(k=3),
                    iaa.MedianBlur(k=3),
                    iaa.AverageBlur(k=3),

                ]),

                iaa.Add((-8, 8), per_channel=1),
                iaa.LinearContrast((0.9,1.1), per_channel=0.5),

                iaa.OneOf([
                    iaa.imgcorruptlike.DefocusBlur(severity=1),
                    iaa.imgcorruptlike.DefocusBlur(severity=1),
                    # iaa.imgcorruptlike.Fog(severity=1),
                    # iaa.imgcorruptlike.Frost(severity=1),
                    # iaa.imgcorruptlike.Snow(severity=1),
                    # iaa.imgcorruptlike.Spatter(severity=1),
                    # iaa.imgcorruptlike.Contrast(severity=1),
                    iaa.imgcorruptlike.JpegCompression(severity=1),
                    iaa.imgcorruptlike.Pixelate(severity=1),
                    # iaa.pillike.FilterBlur(),
                    # iaa.pillike.FilterSmooth(),
                    # iaa.pillike.FilterSmoothMore(),
                    iaa.imgcorruptlike.DefocusBlur(severity=1),
                    iaa.imgcorruptlike.DefocusBlur(severity=1),
                    iaa.imgcorruptlike.JpegCompression(severity=1),
                    iaa.imgcorruptlike.Pixelate(severity=1),

                    iaa.pillike.FilterBlur(),
                    iaa.pillike.FilterSmooth(),
                    iaa.pillike.FilterSmoothMore(),
                    
                ]),

        ],
        random_order=False
    )

    image_aug = seq(images=io_images)
    pil_image = Image.fromarray(image_aug[0])
    return pil_image


# 从四个角点中随机取两个出来进行变换
def aug_image_warp_perspective_old(img, max_x_ratio=0.1, max_y_ratio=0.1):
    """ 使图像轻微的畸变
        img 输入图像, pil_image
        factor 畸变的参数
        size 为图片的目标尺寸
    """
    final_image = img.convert('RGBA')
    image_array = np.frombuffer(final_image.tobytes(), dtype=np.uint8)
    image_array = image_array.reshape((final_image.size[1], final_image.size[0], 4))
    cv_image = copy.copy(image_array[:,:,:3])

    alpha = final_image.split()[-1]
    alpha = np.array(alpha, dtype=np.uint8)
    size_o = [cv_image.shape[1],cv_image.shape[0]]

    pts1 = np.float32([[0,0],[size_o[0],0],[size_o[0],size_o[1]],[0,size_o[1]]])
    

    x_diff = int(size_o[0] * np.random.random() *max_x_ratio )
    y_diff = int(size_o[1] * np.random.random() *max_y_ratio )
    if np.random.randint(2) < 1:
        # change left
        x_diff = -x_diff if np.random.randint(2) < 1 else x_diff
        y_diff = -y_diff if np.random.randint(2) < 1 else y_diff
        p1 = [0, 0 if y_diff < 0 else y_diff]
        p2 = [size_o[0]-x_diff if x_diff < 0 else size_o[0]-x_diff,
                -y_diff  if y_diff < 0  else 0]
        p3 = [
            size_o[0]-x_diff if x_diff < 0 else size_o[0]-x_diff,
            size_o[1]-y_diff  if y_diff < 0  else size_o[1]
        ]
        p4 =[
            0,
            0 + size_o[1] if y_diff < 0 else y_diff + size_o[1]
        ]
        pts2 = np.float32([p1,p2,p3,p4])
    else:
        # change right
        x_diff = -x_diff if np.random.randint(2) < 1 else x_diff
        y_diff = -y_diff if np.random.randint(2) < 1 else y_diff
        p1 = [0, -y_diff if y_diff < 0 else 0]
        p2 = [
            size_o[0]+x_diff if x_diff < 0 else size_o[0]+x_diff,
                0  if y_diff < 0  else y_diff
                ]
        p3 = [
            size_o[0]+x_diff if x_diff < 0 else size_o[0]+x_diff,
                0+size_o[1]  if y_diff < 0  else y_diff+size_o[1]
        ]
        p4 =[
            0,
            size_o[1] -y_diff if y_diff < 0 else  size_o[1]
        ]
        pts2 = np.float32([p1,p2,p3,p4])


    M  = cv2.getPerspectiveTransform(pts1,pts2)
    max_x ,max_y= np.max(pts2,axis=0 )

    dst = cv2.warpPerspective(cv_image,M,(max_x, max_y))
    mask = cv2.warpPerspective(alpha,M,(max_x, max_y))
    return dst, mask, pts2


def rot(img,shape,min_angel=-10,max_angel=10, min_depth_ratio=0.8,max_depth_ratio=1.2):
    """ 使图像轻微的畸变
        img 输入图像
        factor 畸变的参数
        size 为图片的目标尺寸
    """
    size_o = [shape[1],shape[0]]

    left_x_diff = np.random.ra
    size = (shape[1]+ int(shape[0]*sin((float(max_angel )/180) * 3.14)),shape[0])
    # print size
    interval = abs( int( sin((float(angel) /180) * 3.14)* shape[0]))

    pts1 = np.float32([[0,0],[size_o[0],0],[size_o[0],size_o[1]], [0,size_o[1]] ])
    if(angel>0):
        pts2 = np.float32([[interval,0],[0,size[1]  ],[size[0],0  ],[size[0]-interval,size_o[1]]])
    else:
        pts2 = np.float32([[0,0],[interval,size[1]  ],[size[0]-interval,0  ],[size[0],size_o[1]]])

    M  = cv2.getPerspectiveTransform(pts1,pts2)
    dst = cv2.warpPerspective(img,M,size)

    return dst

def main(data_dir):


    image_aug = aug_image(data_dir)

    new_save_dir = data_dir + "_imgaug"
    if not os.path.exists(new_save_dir):
        os.mkdir(new_save_dir)
    # convert_to_json_and_save_img(new_save_dir, image_aug, polygon_aug)

    for i in range(len(image_aug)):
        name = str(i) + ".jpg"
        imageio.imwrite(new_save_dir+os.sep + name,image_aug[i] )


def aug_image_boeing_smoothing(cv_image):

    io_images = [cv_image]

    seq = iaa.Sequential(
        [

                iaa.OneOf([

                    # iaa.imgcorruptlike.DefocusBlur(severity=1),
                    iaa.imgcorruptlike.ZoomBlur(severity=1),
                    # iaa.imgcorruptlike.Fog(severity=1),
                    # iaa.imgcorruptlike.Frost(severity=1),
                    # iaa.imgcorruptlike.Snow(severity=1),
                    # iaa.imgcorruptlike.Spatter(severity=1),
                    # iaa.imgcorruptlike.Contrast(severity=1),
                    iaa.imgcorruptlike.JpegCompression(severity=1),
                    iaa.imgcorruptlike.Pixelate(severity=1),
                    # iaa.pillike.FilterBlur(),
                    # iaa.pillike.FilterSmooth(),
                    # iaa.pillike.FilterSmoothMore(),
                    iaa.AveragePooling(2),

                ]),


                iaa.OneOf([

                    iaa.imgcorruptlike.DefocusBlur(severity=1),
                    iaa.pillike.FilterBlur(),
                    iaa.pillike.FilterSmooth(),
                    # iaa.pillike.FilterSmoothMore(),

                ]),

            # iaa.Rain(speed=(0.1, 0.2)),

            # iaa.SomeOf([2,5],[
            #     # iaa.OneOf([
                    
            #         iaa.GaussianBlur((0, 3.0)),
            #         iaa.MotionBlur(k=3),
            #         iaa.MotionBlur(k=5),

            #     # ]),

            #     # iaa.OneOf([

            #         iaa.MedianBlur(k=(3,11)),
            #         iaa.AverageBlur(k=(3,11)),

            #     # ]),
            #     # iaa.OneOf([
            #         iaa.Sharpen(alpha=(0.3, 0.8), lightness=(0.7, 1.0)),
            #         iaa.Emboss(alpha=(0.2, 0.8), strength=(0.5, 1.1)),

            #     # ]),


            #         iaa.JpegCompression(compression=(70, 95)),




            # ],
            # random_order=True
        # ),



        ],
        random_order=True
    )

    image_aug = seq(images=io_images)
    return image_aug[0]


def enhance_boeing_data(sub_name, data_dir, save_dir):
    for idx, p in enumerate(os.listdir(data_dir + os.sep + sub_name)):
        if np.random.randint(2) == 0:continue
        # if idx > 500:continue

        img_path = data_dir + os.sep + sub_name + os.sep + p
        cv_image = cv2.imread(img_path)
        aug_image = aug_image_boeing_smoothing(cv_image)
        cv2.imwrite(save_dir + os.sep + sub_name + os.sep + p, aug_image)


def boeing_deal(data_dir, save_dir):
    start_c = 0
    end_c = 30
    sub_dir_names = []
    for n in os.listdir(save_dir):
        
        if os.path.isdir(save_dir + os.sep + n):
            if start_c <= int(n) < end_c: 
                sub_dir_names.append(n)

    thread_count  = 10
    # print('Parent process %s.' % os.getpid())
    p = Pool(thread_count)
    for sn in sub_dir_names:

        print(sn)
        p.apply_async(enhance_boeing_data, args=(sn, data_dir,save_dir))
    # print('Waiting for all subprocesses done...')
    p.close()
    p.join()


def filter_by_mean_var(data_dir):
    count = 0
    for sub_name in tqdm(os.listdir(data_dir)):
        sub_dir = data_dir + os.sep + sub_name
        if os.path.isdir(sub_dir):
            for n in os.listdir(sub_dir):
                file_path = sub_dir + os.sep + n
                image = cv2.imread(file_path, 0)
                image = cv2.medianBlur(image, 3)
                var = np.var(image[:, :128])
                if var < 3:
                    os.remove(file_path)
                    count +=1
                    print(count)

if __name__ == "__main__":
    # data_dir = "C:\\Users\\swls\\Desktop\\being\\2019-09-24\\label"

    #5 - 20
    # data_dir = "/home/swls/work_dir/ocr/code/syn/boeing_60k"
    # save_dir = "/home/swls/work_dir/ocr/code/syn/boeing_60k"
    # boeing_deal(data_dir, save_dir)


    # data_dir = "/home/swls/work_dir/ocr/code/syn/boeing_60k"
    # filter_by_mean_var(data_dir)
    pass

# scp swls@192.168.31.167:/home/swls/work_dir/ocr/code/syn/boeing_60k/60/303616.jpg ./
